Running code in main thread from another thread

前端 未结 16 1869
一个人的身影
一个人的身影 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:25

    So most handy is to do sort of:

    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()
            }
        }
    }
    

    And after:

    Dispatch.asyncOnBackground {
        val value = ...// super processing
        Dispatch.asyncOnMain { completion(value)}
    }
    
    0 讨论(0)
  • 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
        })
    
    0 讨论(0)
  • With Kotlin, it is just like this inside any function:

    runOnUiThread {
       // Do work..
    }
    
    0 讨论(0)
  • 2020-11-22 14:31

    More precise Kotlin code using handler :

    Handler(Looper.getMainLooper()).post {  
     // your codes here run on main Thread
     }
    
    0 讨论(0)
  • 2020-11-22 14:31

    Follow this method. Using this way you can simply update the UI from a background thread. runOnUiThread work on the main(UI) thread . I think this code snippet is less complex and easy, especially for beginners.

    AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
    
                //code you want to run on the background
                someCode();
    
               //the code you want to run on main thread
     MainActivity.this.runOnUiThread(new Runnable() {
    
                        public void run() {
    
    /*the code you want to run after the background operation otherwise they will executed earlier and give you an error*/
                            executeAfterOperation();
    
                       }
                    });
                }
            });
    

    in the case of a service

    create a handler in the oncreate

     handler = new Handler();
    

    then use it like this

     private void runOnUiThread(Runnable runnable) {
            handler.post(runnable);
        }
    
    0 讨论(0)
  • 2020-11-22 14:35
    public void mainWork() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                //Add Your Code Here
            }
        });
    }
    

    This can also work in a service class with no issue.

    0 讨论(0)
提交回复
热议问题