当前位置: 动力学知识库 > 问答 > 编程问答 >

android - when asyntask onprogress update running, can't do another task

问题描述:

I am trying to get sensor data from arduino to an android phone using the asynctask onprogress update method. The sensors's data is displayed in a TextView and there are 2 buttons for turning on and off a led.

The problem is that the android phone is getting real-time data from the arduino but i cannot turn on and off the light at the same time. I tried to stop the android phone from getting the sensor's data and tried to turn on and off the light. This is working. Both getting the sensor's data and turning on/off the led are not working simultaneously. Can somebody help me with this problem. It's very important for me as am using it for a project.

The android code:

package com.example.arduinoandroidasyntasktesting6;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;

import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity {

TextView data;

Button On,Off;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

new Sensors().execute("");

data=(TextView)findViewById(R.id.textView2);

On=(Button)findViewById(R.id.buttonOn2);

Off=(Button)findViewById(R.id.buttonOff2);

On.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

new Sensors().execute("?led2=1");

}

});

Off.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

new Sensors().execute("?led2=0");

}

});

}

class Sensors extends AsyncTask<String, byte[], String>{

InputStream nis;

OutputStream nos;

BufferedReader in;

DefaultHttpClient httpclient =new DefaultHttpClient();

URL url;

URLConnection urlconn=null;

InputStreamReader isn;

@Override

protected String doInBackground(String... params) {

try{

while(true){

url =new URL("http://192.168.1.177/"+params[0]);

urlconn=(HttpURLConnection)url.openConnection();

nis = urlconn.getInputStream();

in= new BufferedReader(new InputStreamReader(nis));

String msg = in.readLine();

byte[] thebyte =msg.getBytes();

publishProgress(thebyte);

}

}

catch(IOException e)

{

e.printStackTrace();

}

return null;

}

protected void onProgressUpdate(byte[]... values) {

String command=new String(values[0]);//get the String from the recieved bytes

data.setText(command);

}

}

}

网友答案:

AsyncTasks will be executed one after another. You are starting several async tasks but if the first one does not finish the other ones will never run.

To let them really run in parallel use an executor or use threads.

You could use one async task for the sensing and use threads for on/off buttons.

分享给朋友:
您可能感兴趣的文章:
随机阅读: