Answer the question
In order to leave comments, you need to log in
How to run a method on the main thread from a side thread?
I am writing a program in which it is necessary to check for devices connected to a smartphone. If the device is connected, a picture should appear on the screen. I check with a timer. But I can't show a picture from a neighboring thread. How do I run a method on the main thread via a side thread?
Answer the question
In order to leave comments, you need to log in
You can pass the Handler of the main thread
In the main thread, create:
Handler handler = new Handler(Looper.getMainLooper());
// Передаем его в побочный поток
handler.post(new Runnable() {
public void run() {
// Код для основного потока
// ...
}
});
Observer/Observable from JDK7+. If JDK6, then implement this template yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question