Android: androidui
It is very easy for Android to display the Dialog in the application, but it has never been tried to display the Dialog in the Service. Based on previous experience, UI operations should be performed in the main thread. The local Service is in the main thread, but the Dialog is displayed in the remote service. Does it sound like it should not be in the main thread?
After trying it out, I wrote an AIDL call. The client calls AIDL and a Dialog is displayed on the Service side. The AIDL part is ignored. The Code on the Service side is the same as that on the Activity display dialog.
AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle("TEST"); builder.setMessage("test"); builder.show();
Of course, dialog requires setType (WindowManager. LayoutParams. TYPE_SYSTEM_ALERT );
The result is a tragedy.
Can't create handler inside thread that has not called logoff. prepare ()
What do you mean? To display a Dialog, you need to support message loops. Just give it a message loop. But the problem is... Which thread does this process occur? Later, the debugging found that there would be a thread in every call to the binder, and it was different every time. It is estimated that it was taken from a thread pool.
So how can we add logoff to this thread? There is no way to open only one thread by myself. It seems that there is no problem to display the dailog in a thread with logoff.
Then the question comes again. Why does the display of Dialog require logoff support?
Check the Code:
The original Dialog has
private final Handler mHandler = new Handler();
There is another
mListenersHandler = new ListenersHandler(this);
It seems that both Handler are long on the current thread, so you can understand why show Dialog must be logoff.
Finally, there is another problem. I have always said that UI operations must be performed on the main thread. It is a bit strange to say that the Service is remote, it shows that dialog is a sub-thread of the Service. Is there a relationship between dialog and the main thread? I personally understand that this non-main thread does not operate the UI.
I have seen that both Dialog and Acitivty use WindowManager to add a view (ViewRoot) to the window. It is impossible for all views to belong to only one client, each client has a piece of cake on this window, so it is not surprising that multiple threads will update their views. However, each View can only be operated by one thread. This is my understanding that non-main threads cannot operate the UI, and I do not know if it is correct.
The non-main thread of Android cannot be changed directly.
The thread definition is also two threads in the main thread. variables defined in the main thread can be used in the specific run method, but the UI cannot be modified. You must use the runonuithread method or handler to do this.
An error occurred while changing the UI of non-main android threads.
You can modify the UI only on the main line.
The subthread can post messages to the main thread through handler.