在一个android服务,我已经创建线程(s)做一些后台任务。

我有一个线程需要在主线程的消息队列上发布某个任务的情况,例如一个可运行的。

有没有办法得到主线程的处理程序和post Message/Runnable到它从我的其他线程?


当前回答

压缩代码块如下所示:

   new Handler(Looper.getMainLooper()).post(new Runnable() {
       @Override
       public void run() {
           // things to do on the main thread
       }
   });

这并不涉及传递活动引用或应用程序引用。

芬兰湾的科特林相当于:

    Handler(Looper.getMainLooper()).post(Runnable {
        // things to do on the main thread
    })

其他回答

HandlerThread是更好的选择,以正常的java线程在Android。

创建一个HandlerThread并启动它 从HandlerThread:requestHandler中创建一个Looper Handler 在requestHandler上发布一个可运行任务

从HandlerThread与UI线程通信

为主线程创建一个Looper Handler: responseHandler并重写handleMessage方法 在其他线程(在本例中为HandlerThread)的可运行任务中,调用responseHandler上的sendMessage responseHandler中handleMessage的sendMessage结果调用。 从Message中获取属性并处理它,更新UI

示例:用从web服务接收到的数据更新TextView。由于web服务应该在非ui线程上调用,因此为网络操作创建了HandlerThread。一旦你从web服务获得内容,发送消息到你的主线程(UI线程)处理程序,该处理程序将处理消息并更新UI。

示例代码:

HandlerThread handlerThread = new HandlerThread("NetworkOperation");
handlerThread.start();
Handler requestHandler = new Handler(handlerThread.getLooper());

final Handler responseHandler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
        txtView.setText((String) msg.obj);
    }
};

Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        try {
            Log.d("Runnable", "Before IO call");
            URL page = new URL("http://www.your_web_site.com/fetchData.jsp");
            StringBuffer text = new StringBuffer();
            HttpURLConnection conn = (HttpURLConnection) page.openConnection();
            conn.connect();
            InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());
            BufferedReader buff = new BufferedReader(in);
            String line;
            while ((line = buff.readLine()) != null) {
                text.append(line + "\n");
            }
            Log.d("Runnable", "After IO call:"+ text.toString());
            Message msg = new Message();
            msg.obj = text.toString();
            responseHandler.sendMessage(msg);


        } catch (Exception err) {
            err.printStackTrace();
        }
    }
};
requestHandler.post(myRunnable);

有用的文章:

handlerthreads-and-why-you-should-be-using-them-in-your-android-apps

android-looper-handler-handlerthread-i

所以最方便的方法是:

import android.os.AsyncTask
import android.os.Handler
import android.os.Looper

object Dispatch {
    fun asyncOnBackground(call: ()->Unit) {
        AsyncTask.execute {
            call()
        }
    }

    fun asyncOnMain(call: ()->Unit) {
        Handler(Looper.getMainLooper()).post {
            call()
        }
    }
}

后:

Dispatch.asyncOnBackground {
    val value = ...// super processing
    Dispatch.asyncOnMain { completion(value)}
}

对于Kotlin,你可以使用Anko corountines:

更新

doAsync {
   ...
}

弃用

async(UI) {
    // Code run on UI thread
    // Use ref() instead of this@MyActivity
}
ContextCompat.getMainExecutor(context).execute {
  // do something
}

压缩代码块如下所示:

   new Handler(Looper.getMainLooper()).post(new Runnable() {
       @Override
       public void run() {
           // things to do on the main thread
       }
   });

这并不涉及传递活动引用或应用程序引用。

芬兰湾的科特林相当于:

    Handler(Looper.getMainLooper()).post(Runnable {
        // things to do on the main thread
    })