Android Service Dialog pop-up box
I believe that the first time you implement the Alertdialog popup in the service, you will encounter the application flash back and then report this exception:
Caused by:android.view.windowmanager$badtokenexception:
The following is why this exception, the reason is very simple, because the alertdialog display is dependent on a certain Activity class , so you want to the implementation of the Service to bounce out, need to do the following configuration:
1, after the installation of the general Alertdialog function block, in the alertobj . Show () statement before adding:
Alertobj.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
For example:
Private voidShowhostonlinealert () {FinalAlertdialog dialog =NewAlertdialog.builder (Backgroudservice. This). Create (); Dialog.setcanceledontouchoutside (false); Dialog.requestwindowfeature (Window.feature_no_title); Dialog.getwindow (). Setbackgrounddrawable (NewColordrawable (color.transparent));//set background was transparent Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//statements that need to be added dialog.show (); }
2. Add permissions in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW" ></uses-permission>
Summary: The above practice is to declare that we want to pop up this prompt box is a system prompt box, that is, the global nature of the prompt box, so as long as the phone is turned on, no matter what interface it is now, as long as the call Alterobj.show (), will pop up the prompt box.
Android Service Dialog pop-up box