send message to Thread which is listening for data from network

不打扰是莪最后的温柔 提交于 2019-12-08 10:42:42

问题


I have an Android app which has the following key components

A service which manages a connection thread A connection thread which connects to IRC and listens for messages from the connection A ui which is bound the the service.

I have a button on the UI and I need to send a message to the irc server when the button is clicked. My idea was to spawn a handler on the connection thread, get a handle to it in my service and then send messages from the UI to the services and from there to the thread.

When I created a handler at class level in the thread I get a networkonuiexception. I moved instantiation of my handler to the run() method and I get told to use a Looper.prepare(). I feel that I have approached this wrong and I am looking for advice on how best to manage this.

Any help would be appreciated.


回答1:


When i started to learn android, i wrote this sample code to handle messages in thread by using looper:

public class MainActivity extends Activity {

    Handler mHandler,childHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHandler = new Handler(){
            public void handleMessage(Message msg) {
                TextView tv = (TextView) findViewById(R.id.displayMessage);
                tv.setText(msg.obj.toString());
            }
            };
        new LooperThread().start();
    }



    public void onSend(View v){
        Message msg = childHandler.obtainMessage();
        TextView tv = (TextView) findViewById(R.id.messageText);
        msg.obj = tv.getText().toString();


        childHandler.sendMessage(msg);
    }
    @Override
    protected void onStart() {
        super.onStart();
        LooperThread ttTest = new LooperThread();
        ttTest.start();
    }

    class LooperThread extends Thread {

        final int MESSAGE_SEND = 1;

        public void run() {
            Looper.prepare();
            childHandler = new Handler() {
                public void handleMessage(Message msg) {
                      Message childMsg =  mHandler.obtainMessage();
                      childMsg.obj = "child is sending "+(String)msg.obj;
                      mHandler.sendMessage(childMsg);
                }
            };
            Looper.loop();
        }
    }
}

Its a sample code to send message to thread on button press, then thread again send message back by adding some string to main activity. You can manipulate this code according to your need.

Activity by default have looper, so no need to write looper for activity.

Threads do not have looper by default, so we need write looper, its some kind of queue to store messages.

UPDATE:

XML code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <EditText
        android:id="@+id/messageText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Input message">
         <requestFocus />
    </EditText>
    <TextView
        android:id="@+id/displayMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </TextView>

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onSend"
        android:text="Send" />

</LinearLayout>


来源:https://stackoverflow.com/questions/16104481/send-message-to-thread-which-is-listening-for-data-from-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!