Android shows dialog is a simple thing in the app, but has never tried to show dialog in the service. Based on previous experience UI operations to be in the main thread, the local service is the main thread, but the remote service inside shows dialog, does that sound like it should not be in the main thread?
Try to know, wrote a aidl call, client side to call Aidl, on the service side is to show a dialog. The aidl part is ignored. The code on this side of the service is the same as the activity shows dialog.
Alertdialog.builder Builder = new Alertdialog.builder (mcontext); Builder.settitle ("TEST"); Builder.setmessage ("test"); Builder.show ();
Of course dialog to Settype (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
As a result, it's tragic.
Can ' t create handler inside thread that have not called looper.prepare ()
What do you mean, subjective understanding, to show a dialog need to have a message loop support, give it a message loop just fine. But here's the problem ... Which thread does this process occur on, and then the debug finds that each binder call comes up with a thread that appears, and that it's not the same every time, and is estimated to be taken from a thread pool.
How to add looper to this thread, no way can only open a thread, sure enough in a thread with Looper to show Dailog seems to be no problem.
So the question comes again, why does showing dialog need Looper support?
Look at the code:
The original dialog has a
Private final Handler Mhandler = new Handler ();
There's another one.
Mlistenershandler = new Listenershandler (this);
It seems that these two handler are all in the current thread, then understand why show dialog must looper.
Finally, there is a problem, has been said that the UI operation must be in the main thread, the above-mentioned situation is a bit strange, the display service is remote, show dialog is a sub-thread of service, and the main thread has a half-dime relationship? In my own understanding, this non-main thread does not seem to be an absolute way of manipulating the UI.
Think again, before you have seen, in fact, whether it is dialog or acitivty is essentially through the WindowManager to the window to add a view (Viewroot), all the view can not be only a client, Each client has a slice of the window, so it's not surprising that multiple threads will be updating their view. Only one thread can operate on each view itself. This is my understanding that the non-main thread cannot manipulate the UI, and I don't know if it is correct.
Android knowledge about non-main thread not operating UI