android - when asyntask onprogress update running, can't do another task -
i trying sensor data arduino android phone using asynctask onprogress update method. sensors's data displayed in textview
, there 2 buttons turning on , off led.
the problem android phone getting real-time data arduino cannot turn on , off light @ same time. tried stop android phone getting sensor's data , tried turn on , off light. working. both getting sensor's data , turning on/off led not working simultaneously. can me problem. it's important me using 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 string recieved bytes data.settext(command); } } }
asynctasks executed 1 after another. starting several async tasks if first 1 not finish other ones never run.
to let them run in parallel use executor or use threads.
you use 1 async task sensing , use threads on/off buttons.
Comments
Post a Comment