I'm new to Android development (coming from C/C++, Win32 world).
I'm trying to write a single-threaded service that should process various kinds of callbacks (system events, timers).
What I need is a (sort of) emulation of a modal message-processing loop. That is, I want to be able to call some function that waits until some specific criteria is satisfied. However, while waiting, the thread should process all the events "in background". Actually the condition we're waiting for is triggered by one of the callbacks.
Moreover, those "modal message-processing loops" may be nested. That is, while waiting for one criteria, from within a callback I may need to run another message-processing loop, waiting to another criteria.
And I don't want multi-threading. Everything should be processed within single thread.
From what I found in the documentation the most close match to what I need is using something like Handler.getLooper().loop()
to run the loop, and Handler.getLooper().quitSafely()
to trigger loop break.
However the documentation is somewhat confusing. It is unclear if it's allowed to call this function recursively. Also, it's unclear if after quitSafely()
the loop can be used again. The documentation states that quit()
is indeed the last function called for the looper, after which it's no more usable, however for quitSafely()
it states that:
pending delayed messages with due times in the future will not be delivered before the loop terminates.
Does it mean that after the loop terminates - it's usable again?
If not, what are the other options available? Can I control the message loop explicitly? Something similar to GetMessage
/ DispatchMessage
in Win32?