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 queue, for example a Runnable.
Is there a way to get Handler of the main thread and post Message/Runnable to it from my other thread?
NOTE: This answer has gotten so much attention, that I need to update it. Since the original answer was posted, the comment from @dzeikei has gotten almost as much attention as the original answer. So here are 2 possible solutions:
1. If your background thread has a reference to a
Contextobject:Make sure that your background worker threads have access to a Context object (can be the Application context or the Service context). Then just do this in the background worker thread:
2. If your background thread does not have (or need) a
Contextobject(suggested by @dzeikei):