很少說,直接上車。。。android
1 public class MyReceiver extends BroadcastReceiver { 2 @Override 3 public void onReceive(final Context context, Intent intent) { 4 AlertDialog.Builder builder = new AlertDialog.Builder(context); 5 builder.setTitle("提示"); 6 builder.setMessage("肯定打開主界面嗎?"); 7 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 8 @Override 9 public void onClick(DialogInterface dialog, int which) { 10 Intent intent1 = new Intent(context, MainActivity.class); 11 12 //在廣播接收器中啓動活動,必定要給Intent加入FLAG_ACTIVITY_NEW_TASK標誌 13 intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 14 context.startActivity(intent1); 15 } 16 }); 17 18 AlertDialog dialog = builder.create(); 19 20 //須要把對話框的類型設爲TYPE_SYSTEM_ALERT,不然對話框沒法在廣播接收器裏彈出 21 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 22 dialog.show(); 23 } 24 }
注意:把對話框的類型設爲了TYPE_SYSTEM_ALERT, 這樣彈出的就是一個系統級別的對話框,所以必須聲明android.permission.SYSTEM_ALERT_WINDOW權限。最後不要忘記註冊廣播接收器哦。ide