寫好Alter功能塊後,在alter.show()語句前加入:java
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
注:alter爲AlertDialog類型對象android
而後在AndroidManifest.xml中加入權限:學習
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
下面進行簡單的解釋:ui
若是隻在Service中寫入常在Activity中使用的建立Alter的代碼,運行時是會發生錯誤的,由於Alter的顯示須要依附於一個肯定的Activity類。而以上作法就是聲明咱們要彈出的這個提示框是一個系統的提示框,即全局性質的提示框,因此只要手機處於開機狀態,不管它如今處於何種界面之下,只要調用alter.show(),就會彈出提示框來。this
demo以下:spa
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are 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學習 - 對話框 (AlertDialog & ProgressDialog)
http://android.blog.51cto.com/268543/333769code