S
S
Satangelus2018-07-17 03:17:30
Android
Satangelus, 2018-07-17 03:17:30

How to check if a procedure is running on the main thread or not?

I have one procedure that, when called not from the main thread, issues an exception.
The procedure itself calls the code from the SDK which is critical for execution in the main thread. But by purpose and essence, the code can be called from anywhere.
Can't create handler inside thread that has not called Looper.prepare()
Wrapping in runOnUiThread helped.
I would like to patch the SDK, determining if the code was launched not from the main thread, throw out my own exception with a normal description of the situation, so that others do not run into the same rake and wonder where the jamb is.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-07-17
@Satangelus

First, it is worth designing the program so that the code cannot run anywhere. Second, you can catch exceptions. Finally, you can check the construction

if (Looper.getMainLooper().isCurrentThread()) {
    // UI поток
}

S
Sergey, 2018-07-17
@red-barbarian

nothing is normal)
some things are specially made to be called on the main thread.
if you really have a desire, then make a wrapper and name it differently.
it is also possible that the procedure (method) does not need a ui-thread, but simply a Looper. Then add a looper.
It is not clear from the question what is required.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question