Android how to runOnUiThread in other class?

后端 未结 6 1944
再見小時候
再見小時候 2020-11-28 08:22

In my application, in MainActivity, there is a thread which works fine. But when I call another class to get data from the server I can\'t run on a thread. See code exampl

6条回答
  •  再見小時候
    2020-11-28 09:22

    You might want to take a look at AsyncTask. Even though it's not the best solution, it will help you get started.

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

    EDIT

    I don't see why using an AsyncTask is not a solution for you but anyway. You can hold a Handler class that is initialized in the UI thread. Then using this Handler you can post back messages to the UI in the form of a runnable. So all you need to do is instantiate a new Handler object when you are in the UI thread (before you start your new one) and then share that with your other class. When you are done, you can use that instance to post a message back to the UI thread using the post method. Check out the documentation of the Handler class for more details:

    http://developer.android.com/reference/android/os/Handler.html

提交回复
热议问题