Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将消息发送到正在侦听网络数据的线程_Android_Service_Irc - Fatal编程技术网

Android 将消息发送到正在侦听网络数据的线程

Android 将消息发送到正在侦听网络数据的线程,android,service,irc,Android,Service,Irc,我有一个Android应用程序,它有以下关键组件 管理连接线程的服务 连接到IRC并侦听来自连接的消息的连接线程 绑定到服务的ui 我在UI上有一个按钮,我需要在单击按钮时向irc服务器发送消息。我的想法是在连接线程上生成一个处理程序,在我的服务中获得一个句柄,然后从UI向服务发送消息,再从UI向线程发送消息 当我在线程中创建类级别的处理程序时,我得到一个NetworkOnIEException。我将处理程序的实例化移动到run()方法,并被告知使用Looper.prepare()。我觉得我处理

我有一个Android应用程序,它有以下关键组件

管理连接线程的服务 连接到IRC并侦听来自连接的消息的连接线程 绑定到服务的ui

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

当我在线程中创建类级别的处理程序时,我得到一个NetworkOnIEException。我将处理程序的实例化移动到run()方法,并被告知使用Looper.prepare()。我觉得我处理这件事是错误的,我正在寻求如何最好地处理这件事的建议


任何帮助都将不胜感激。

当我开始学习android时,我编写了以下示例代码,使用
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();
        }
    }
}
这是一个示例代码,用于在按下按钮时向线程发送消息,然后线程通过向主活动添加一些字符串再次发送消息。您可以根据需要操作此代码

默认情况下,活动具有循环器,因此无需为活动编写循环器

线程默认情况下没有循环器,所以我们需要写循环器,它是存储消息的某种队列

更新:

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>

Hi Sunny。谢谢你的回复。对我来说,这种方法的问题是线程处于while()循环中等待消息,因此我认为不应该添加prepare()或loop()