D
D
DivelNick2016-06-13 17:21:43
Java
DivelNick, 2016-06-13 17:21:43

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

3 answer(s)
M
MikkiMouse, 2016-06-13
@DivelNick

You can pass the Handler of the main thread
In the main thread, create:

Handler handler = new Handler(Looper.getMainLooper());
// Передаем его в побочный поток

In the side we use:
handler.post(new Runnable() {
  public void run() {
    // Код для основного потока
    // ...
  }
});

Or, as advised above, it's easier, through runOnUiThread

R
Rou1997, 2016-06-13
@Rou1997

runOnUiThread.

A
Alexander Kosarev, 2016-06-14
@jaxtr

Observer/Observable from JDK7+. If JDK6, then implement this template yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question