Running code in main thread from another thread

前端 未结 16 2089
一个人的身影
一个人的身影 2020-11-22 13:50

In an android service I have created thread(s) for doing some background task.

I have a situation where a thread needs to post certain task on main thread\'s message

16条回答
  •  日久生厌
    2020-11-22 14:28

    A condensed code block is as follows:

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

    This does not involve passing down the Activity reference or the Application reference.

    Kotlin Equivalent:

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

提交回复
热议问题