I have a method in an object executing on a secondary thread. In the event that an error occurs, this method wants to tell the object's delegate to respond to this. The response must take place on the main thread (i.e. UI thread).
How do I call through t开发者_开发技巧o the main thread from my secondary thread? My object's class does not extend Activity
, and I have no obvious Activity
object at hand.
Use Android handler. Here's an example
http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
Of course, you will have to pass the Handler into your 2nd thread somehow.
You can create a variable Context in the second class and then pass on the activity reference to this class in that variable.
Later you can use context in a similar way as activity.
There are a few strategies to achieve that: AsyncTask, Handler, View.post(). They are all described in painless threading article.
精彩评论