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
If you run code in a thread, e.g. do delaying some action, then you need to invoke runOnUiThread
from the context. For example, if your code is inside MainActivity
class then use this:
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
myAction();
}
});
If your method can be invoked either from main (UI thread) or from other threads you need a check like:
public void myMethod() {
if( Looper.myLooper() == Looper.getMainLooper() ) {
myAction();
}
else {
}