TCP client in Android phone: run continuously, display image based on message received from server -
i have tcp client running in android phone. connects , receives data windows application. once receive message, parse , try display image. have keep checking new messages windows app.
the client thread keeps running. when message received, pass main activity, parse not able display image. want make client check new message every 100 ms. @ moment, logcat gets flooded thread keeps running , not able see contents of logcat.
basically want run client, keep checking new messages every 100 ms, when new message there, pass main activity, parse , display image. please go through code below , suggest corrections or better approach if necessary.
the client code below.
main activity:
public class tcplisten extends activity implements tcplistener { private textview mtitle; public string data[] = new string[2]; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setcontentview(r.layout.main); // set window layout requestwindowfeature(window.feature_custom_title); setcontentview(r.layout.main); getwindow().setfeatureint(window.feature_custom_title, r.layout.custom_title); // set custom title mtitle = (textview) findviewbyid(r.id.title_left_text); mtitle.settext(r.string.app_name); mtitle = (textview) findviewbyid(r.id.title_right_text); //tcpservicehandler handler=new tcpservicehandler(this); //handler.execute("192.168.62.23"); tcpservicehandler handler = new tcpservicehandler(this,this); thread th = new thread(handler); th.start(); } public string[] callcompleted(string source){ log.d("tcp", "std parser " + source); mtitle.settext(source); //string data[] = new string[2]; //if (source.matches("<msg><n>.*</n><v>.*</v></msg>")) { document doc = null; try{ documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); doc = (document) db.parse(new bytearrayinputstream(source.getbytes())); nodelist n = doc.getelementsbytagname("n"); node nd = n.item(0); string msgname = nd.getfirstchild().getnodevalue(); nodelist n1 = doc.getelementsbytagname("v"); node nd1 = n1.item(0); string tmpval = nd1.getfirstchild().getnodevalue(); data[0] = msgname; data[1] = tmpval; log.d("tcp", "inside std parser " + data[0] + " " + data[1]); actionondata(data[0], data[1]); } catch(exception e){ e.printstacktrace(); } log.d("tcp", "just outside std parser " + data[0] + " " + data[1]); return data; //} else log.d("tcp", "message in wrong format " + source); //mtitle.settext("message in wrong format " + source); //return data; } //function display driver messages/images based on individual messages public void actionondata(string name, string value) { string tempname = name; string tempval = value; //while (true) { if(tempname.equals("shiftdirection") && tempval.equals("1")) { log.d("tcp","in actionondata " + data[0] + " " + data[1]); mtitle.settext("change next higher gear"); intent myintent = new intent(); myintent.setclassname("com.example.android.tcplisten", "com.example.android.tcplisten.images"); //myintent.putextra("change gear", "shift next gear!"); // key/value pair, key needs current package prefix. startactivity(myintent); try { wait(3000); } catch(interruptedexception e) { system.out.println("interruptedexception caught"); } } else if(tempname.equals("vehiclespeed") && tempval.equals("120")) { log.d("tcp","in actionondata " + data[0] + " " + data[1]); mtitle.settext("drive man"); //intent myintent = new intent(); //myintent.setclassname("com.example.android.tcplisten", "com.example.android.tcplisten.images"); ////myintent.putextra("change gear", "shift next gear!"); // key/value pair, key needs current package prefix. //startactivity(myintent); } else log.d("tcp", "just show image"); //} } }
interface:
public interface tcplistener { public string[] callcompleted(string msg); }
thread:
public class tcpservicehandler implements runnable { tcplistener _listener; private activity _act; public tcpservicehandler(tcplistener listener, activity act){ _listener = listener; _act = act; } public synchronized void run() { // todo auto-generated method stub //if(socket==null){ try { //inetaddress serveraddr = inetaddress.getbyname("192.168.178.25"); socket socket = new socket("192.168.2.103", 1200, true); // while(true){ try { bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream())); final string str = in.readline(); this._act.runonuithread(new runnable(){ public void run() { _listener.callcompleted(str); } }); } catch(exception e){ e.printstacktrace(); } } } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
since asked suggestions better approaches, might consider having service tcp listener instead of activity , when message detected open activity shows image want. it's more complicated, seems more in keeping activities , services traditionally used for, if i'm understanding correctly. guess depends on things how long wait between messages , what's going on while user waiting.
Comments
Post a Comment