如何在Android UI Thread中收听传入事件

时间:2012-07-02 10:42:25

标签: android multithreading communication

阅读了大量的片段和教程后,我仍然(或者甚至更多)对这条道路感到困惑。我需要一个线程/ backgroundtask,它侦听套接字上的传入事件并向UIThread报告任何incomings。选择的首选方式是什么?自己的线程还是多任务处理?将数据传输到主线程的最佳方法是什么?

Thanx对此事的任何想法。

此致 马库斯

考虑到下面的答案,我尝试了以下内容:

MainActivity:

public class MainActivity extends Activity {


Handler handler = new Handler() {

    @Override
       public void handleMessage(Message msg) {
            toastSomething();
      };
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    threadstarter();
}


protected void threadstarter() {

    super.onStart();

    Thread backgroundthread = new Thread(new WorkerThread(handler));
    backgroundthread.start();
}

public void toastSomething() {
    Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
} }

我的可运行:

public class WorkerThread implements Runnable {

Handler messageHandler;

WorkerThread(Handler incomingHandler) {
    messageHandler = incomingHandler;
}

public void run() {

    while (true) {

        for (int i = 0; i <= 100000; i++) {
            // wait a moment
        }
        messageHandler.sendEmptyMessage(1);
    }
} }

我的布局只包含一个额外的复选框:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox" />

好的是,吐司出现了。糟糕的是,cehckbox没有响应,应用程序崩溃很快。不是吗,应该怎么做?

编辑:在WorkerThread中sendMessage中的msg似乎是麻烦制造者,因为异常说,消息全部被读取使用?

4 个答案:

答案 0 :(得分:0)

只能从后台与UI线程进行通信

runOnUIThread(new Runnable {.// your ui stuff goes here.});

handler.post(new Runnable{.// your ui stuff goes here.});

我无法想到其他任何事情..这两种情况在所有情况下都非常方便..

在ru

答案 1 :(得分:0)

你可以像这样从后台线程到UIThread

进行通信
Runnable runnable = new Runnable() 
{
@Override
public void run() {
    for (int i = 0; i <= 10; i++) {
        handler.post(new Runnable() {
            @Override
                public void run() {
                    progress.setProgress(value);
                }
            });
        }
    }
};

答案 2 :(得分:0)

您可以使用AsyncTask类,并在onPreExecute(...)onPostExecute(...)onProgressUpdate(...)方法内的UI线程上发布。

另一种方法是使用新线程进行后台工作,并将Runnable发布到Handler

如果在UI线程上实例化Handler,那么发布到处理程序的所有内容都将在UI线程上运行。如果您在后台线程上实例化处理程序,那么您发布到处理程序的所有内容都将在后台线程上运行。

答案 3 :(得分:0)

这是代码,终于可以了:

UIThread:

public class MainActivity extends Activity {

    public static final String LOG_TAG = "UIThread";

    Handler handler = new Handler() {

        @Override
           public void handleMessage(Message msg) {
                toastSomething();
                //Log.v(LOG_TAG, "main thread");
          };
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        threadstarter();
    }

    protected void threadstarter() {

        super.onStart();

        Thread backgroundthread = new Thread(new WorkerThread(handler));
        backgroundthread.start();
    }

    public void toastSomething() {
        Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
    }
}

的WorkerThread:

public class MainActivity extends Activity {

    public static final String LOG_TAG = "UIThread";

    Handler handler = new Handler() {

        @Override
           public void handleMessage(Message msg) {
                toastSomething();
                //Log.v(LOG_TAG, "main thread");
          };
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        threadstarter();
    }

    protected void threadstarter() {

        super.onStart();

        Thread backgroundthread = new Thread(new WorkerThread(handler));
        backgroundthread.start();
    }

    public void toastSomething() {
        Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
    }
}
某人HTH。 Thanx to all for the input。