After writing the ALTER function block, add it before the Alter.show () statement:
Alert.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
NOTE: Alter is an Alertdialog type Object
Then add the permissions in the Androidmanifest.xml:
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW" ></uses-permission>
The following is a simple explanation:
If you write only the code of the create alter that is used in the activity in the service, the runtime is wrong, because the alter display needs to be dependent on a certain activity class. And 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 Alter.show (), will pop up the prompt box.
The demo is as follows:
Alertdialog.builder builder = new alertdialog.builder (this); builder.setmessage ("is you sure you want to exit? ") .setcancelable (False) .setpositivebutton ("Yes", new dialoginterface.onclicklistener () { public void onclick (DialogInterface Dialog, int id) { myactivity.this.finish (); } }) .setnegativebutton ("No", new dialoginterface.onclicklistener () { public void onclick (DIaloginterface dialog, int id) { dialog.cancel (); } }); alertdialog alert = builder.create (); alert.getwindow ( ). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); alert.show ();
Android UI Learning-dialog box (Alertdialog & ProgressDialog)
http://android.blog.51cto.com/268543/333769
Android in Service,broadcast Onreceiver () Pop-Up Dialog dialog box