In my android application I would like to continuously read incoming data (using Telnet protocol). I am able to read one line (and then the app runs at idle), and I'm wondering how to create a simple endless main loop.
I think it would make sense to put a loop around this line:
telnetThread.start();
I already tried to implement a looper, but without any success.
This is my code:
package com.example.clienttel;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import org.apache.commons.net.telnet.TelnetClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends ActionBarActivity {
// Variables
public final String ADDRESS = "194.66.82.11";
public final int PORT = 50100;
public String NMEA = null;
public final String TAG = "TestApp";
public boolean first = true;
public MyTelnetClass mtc;
public Thread telnetThread;
// Handler in mainthread
Handler handler = new Handler() {
public void handleMessage(Message msg) {
String dataString = "";
Bundle bundle = msg.getData();
Log.d("handleMessage", bundle.toString());
if (bundle.containsKey("outgoingString")) {
dataString = bundle.getString("outgoingString");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createThread();
}
private void createThread() {
final Runnable runnable = new Runnable() {
public void run() {
try {
mtc = new MyTelnetClass();
mtc.mhandler = handler;
mtc.run();
Bundle b = new Bundle();
b.putString("TCPThread", "visible");
Message m = handler.obtainMessage();
m.setData(b);
handler.sendMessage(m);
} catch (Exception e) {
Log.e("createTCPThread", "exception: " + e.getMessage());
Bundle b = new Bundle();
b.putString("TCPThread", "unvisible");
Message m = handler.obtainMessage();
m.setData(b);
handler.sendMessage(m);
}
}
};
// create the thread needed and run it, LOOP HERE?
telnetThread = new Thread(runnable);
telnetThread.start();
}
// catch NMEA-sentences and send them using mhandler
class MyTelnetClass {
public Handler mhandler = null;
public void run() {
TelnetClient telnet = new TelnetClient();
Log.i(TAG, "telnetThread active");
if (first) {
// Connect To Server in 1st Iteration
try {
telnet.connect(ADDRESS, PORT);
} catch (IOException e) {
e.printStackTrace();
}
first = false;
}
// Process NMEA-sentences
InputStream inStream = telnet.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(inStream));
try {
NMEA = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
// Handler in MyTelnetClass to send back
Bundle b = new Bundle();
b.putString("outgoingString", NMEA);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}
}
}
What would be an efficient way to handle that?
EDIT:
I put the looper "around" the run(), but it just executes once (I'm not sure if the loop can work this way):
public void run() {
try {
Looper.prepare(); // HERE...
Log.d(TAG,"executing run");
mtc = new MyTelnetClass();
mtc.mhandler = handler;
mtc.run();
Bundle b = new Bundle();
b.putString("TCPThread", "visible");
Message m = handler.obtainMessage();
m.setData(b);
handler.sendMessage(m);
Looper.loop(); // ...AND HERE
} catch (Exception e) {
Log.e("createTCPThread", "exception: " + e.getMessage());
Bundle b = new Bundle();
b.putString("TCPThread", "unvisible");
Message m = handler.obtainMessage();
m.setData(b);
handler.sendMessage(m);
}
}
you have created a thread, this is the start for you but it will run once.
if you want him to run in a loop you need to put loop.
the loop need to be in the run()
method, from the start to it's end, or as much as you need of it.
if you write a loop where you commended it will be a waste of time(/memory) and not effective, it will create the same thread over and over.
if you do it where the telnetThread.start();
is, it will cause to tons of thread and they all will run in about the same time.
if you do it in the run()
function it will run infinite times (unless you have a condition) and will run only after the last run of the loop ended.