Reprint: http://blog.csdn.net/huxueyan521/article/details/8954844
When you add a view to a window by Windowmananger, you need to set the alert parameter and add alert permissions
New windowmanager.layoutparams (layoutparams.wrap_content, layoutparams.wrap_content, LayoutParams.TYPE_ System_alert, layoutparams.flag_not_focusable, pixelformat.transparent); = Gravity.left | Gravity.top;
Permissions:
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
dialog is dependent on activity. However, in the app, it is often necessary to do some background operations in the Service and display a dialog to inform the user when a critical condition is met.
At this point dialog cannot start directly from the service and will report an error android.view.windowmanager$badtokenexception:unable to add window.
There are two ways to resolve this:
1. Displays the dialog of a system boundary, that is, the dialog of the global nature. This dialog can be bounced out of any interface. However, this dialog does not correspond to the home key and the return key, which forces the user to dialog after the action is made.
The use method is that the type of window that is set dialog before the Dialog.show () statement is the system alert type. As follows:
Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.show ();
You also need to add permissions in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
2. Add a transparent activity to the back of the dialog. That is, a transparent activity is displayed first, and the dialog is displayed in the context using activity. It should be noted that activity in the destroy must be dialog to dismiss off, otherwise the activity disappears but dialog still, will crash.
Transparent activity is created by adding theme to the Androidmanifest.xml:
<activity android:name= "com.a.b.activity" android:theme= "@android: style/ Theme.translucent "> </activity>
In addition, you need to set the activity without Actionbar, and the Setup method is added in the activity's OnCreate method:
Requestwindowfeature (Window.feature_no_title);
The [Android Pro] Service displays a dialog or displays the view via Windowmanage