J'ai un client TCP en cours d'exécution dans le téléphone Android. Il connecte et reçoit des données d'une application Windows. Une fois que je reçois un message, je l'analyse et j'essaie d'afficher une image. Je dois continuer à vérifier les nouveaux messages de cette application Windows.Client TCP dans le téléphone Android: exécuter en continu, afficher l'image en fonction du message reçu du serveur
Le thread client reste actif. Quand un message est reçu, je le passe à l'activité principale, je l'analyse mais je ne suis pas capable d'afficher l'image. Je veux que le client vérifie le nouveau message toutes les 100 ms. À l'heure actuelle, le LogCat est inondé pendant que le fil continue à fonctionner et je ne suis pas capable de voir vraiment le contenu de LogCat. Fondamentalement, je veux exécuter le client, continuer à vérifier les nouveaux messages toutes les 100 ms, lorsqu'un nouveau message est là, le transmettre à l'activité principale, l'analyser et afficher une image. Veuillez passer en revue le code ci-dessous et suggérer des corrections ou une meilleure approche si nécessaire.
Le code client est ci-dessous.
Activité principale:
public class TCPListen extends Activity implements TCPListener {
private TextView mTitle;
public String data[] = new String[2];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
// Set up the 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 to 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 to next higher gear");
Intent myIntent = new Intent();
myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
//myIntent.putExtra("Change gear", "Shift to next gear!"); // key/value pair, where 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 like a man");
//Intent myIntent = new Intent();
//myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
////myIntent.putExtra("Change gear", "Shift to next gear!"); // key/value pair, where key needs current package prefix.
//startActivity(myIntent);
} else Log.d("TCP", "Just show an image");
//}
}
}
Interface:
public interface TCPListener {
public String[] callCompleted(String msg);
}
Discussion:
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();
}
}
}