发送消息到正在侦听网络数据的线程 [英] send message to Thread which is listening for data from network

查看:63
本文介绍了发送消息到正在侦听网络数据的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下关键组件的Android应用

I have an Android app which has the following key components

管理连接线程的服务 连接到IRC并侦听来自连接的消息的连接线程 服务绑定的用户界面.

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.

我在UI上有一个按钮,单击该按钮时需要向irc服务器发送消息.我的想法是在连接线程上生成处理程序,在我的服务中获取它的句柄,然后将消息从UI发送到服务,再从那里发送到线程.

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.

当我在线程的类级别创建处理程序时,我得到了networkonuiexception.我将处理程序的实例移动到run()方法,并被告知要使用Looper.prepare().我觉得我处理了这个错误,我正在寻找有关如何最好地解决此问题的建议.

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.

任何帮助将不胜感激.

推荐答案

当我开始学习android时,我使用looper编写了此示例代码来处理线程中的消息:

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.

更新:

XML代码

<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>

这篇关于发送消息到正在侦听网络数据的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆