简体   繁体   中英

Looping a Thread

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.

try the follow code:

   {
                new Thread () {
                    public void run() {
                        Message msg = Message.obtain();
                        msg.what=1;         
                        try {
                            openTelnetConnection(url, mmsgH); 
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();                        }                                                                                       
                            }
                }.start();         
            }
        private void openTelnetConnection(String urlStr, Handler mhandler) throws IOException {
            ProtocolConnection protConn = null;
            String line = null;
            try {

                // Get your CONNECTION
                BufferedReader is =
                        new BufferedReader(new InputStreamReader(protConn.getInputStream()));                   

                while ((line = is.readLine( )) != null) {

                        Message msg = Message.obtain();
                        msg.what=1;  
                        msg.obj=line;                       
                        mhandler.sendMessage(msg);
                }               
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch( SocketTimeoutException e){
                e.printStackTrace();
            } catch (IOException e) {
                Log.d(TAG, "IOERR " +e);
                e.printStackTrace();
                Message msg = Message.obtain();
                    msg.what=2;
                    BufferedInputStream in = new BufferedInputStream(httpConn.getErrorStream());            
                    line =new String(readStream(in));
                    msg.obj=line;
                    mhandler.sendMessage(msg);                
            }
            finally {protConn.disconnect();}
        }

As @ChrisStratton and @amitfarag said, a simple while() -loop did exactly what I required. No need for loopers in my case:

while (true) {
                    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);
                        e.printStackTrace();
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM