how to get result from thread in android?

后端 未结 3 678
小蘑菇
小蘑菇 2021-01-21 09:07

I have a main class, a worker thread class and it separated. In main thread, I pass input to worker thread and ask for it to work. When it finish, I want it to send back result

3条回答
  •  抹茶落季
    2021-01-21 09:32

    Maybe you should try AsynTask, it is designed for just that:

     private class MyThread extends AsyncTask {
         protected Long doInBackground(URL... urls) {
                // Calculate and return retulst
         }
    
    
         protected void onPostExecute(Long result) {
             // This is executed in main Thread, use the result
         }
     }  
    

    You execute this thread like this:

    new MyThread().execute(params, ...);
    

    See http://developer.android.com/reference/android/os/AsyncTask.html

提交回复
热议问题