3

文档说 execute() 必须从 UI 线程调用。但是,由于我每隔几秒钟更新一次图像,所以我使用的是 Runnable。在那里,我定义了必须执行的操作。Starting(execute())ASyncTask 就是其中之一。但是,由于 ASyncTask 不应该从 UI 线程之外的任何地方调用,我该如何继续?

4

3 回答 3

5

只需在 Runnable 中添加 runOnUiThread 即可启动 AsyncTask :

private Runnable updateImages= new Runnable() {
    public void run() {
        runOnUiThread(new Runnable() {
            public void run() { 
                // call your asynctask here 
            }
        }
    });
    //////YOUR CODE HERE
}
于 2012-06-28T06:01:23.513 回答
1

可能重新设计您的项目以仅使用AsyncTasks 而不是Runnable. 我也不确定AsyncTask这种行为有多喜欢,但我之前在 AsyncTasks 中更改了一些 UI 内容。

于 2012-06-28T05:53:46.463 回答
0

干得好。

private Handler mHandler = new Handler(Looper.getMainLooper());

private Runnable updateImages= new Runnable() {
    public void run() {
        .........
        .........
        ......... 

        mHandler.post(new Runnable() {
            public void run() {
               call your asynctask here
            }
        });
    }
};
于 2012-06-28T05:56:26.943 回答