Explanation of Android Global Popup Dialog SYSTEM_ALERT_WINDOW Permissions

  • 2021-08-28 21:06:39
  • OfStack

In the project, in order to realize the monitoring of account multi-device login, a dialog box prompt needs to pop up on the device when one account logs in on other devices, so the global dialog box is used

Option 1.

1. Global pop-up dialog boxes are sometimes used in development, but permissions must be applied in manifest:


<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

2. Create Dialog


AlertDialog.Builder builder=new AlertDialog.Builder(this);
  builder.setIcon(R.drawable.logo_mini);
  builder.setTitle(" Off-line notification ").setMessage(" This account is in another 1 Taiwan Android Log in on the device. ")
  .setPositiveButton(" Re-landing ", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    //do somthing
   }
  }).setNegativeButton(" Quit ",new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    Intent i=new Intent(CoreService.this,LoginActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
   }
  });
  alertDialog = builder.create();
  alertDialog.setCancelable(false);
  alertDialog.setCanceledOnTouchOutside(false);
  alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  alertDialog.show();

Note to set the Window type of Dialog to WindowManager.LayoutParams.TYPE_SYSTEM_ALERT.

Option 2.

Problems arising from the adoption of scenario 1:

When installing the application, the user will ask whether the user is authorized or not

At the same time, the system pop-up box is prohibited by default on Xiaomi mobile phone, and the system pop-up box in the application will not be able to pop up

Can you pop up a prompt box to prompt the user without applying for system permission?

Here, we will change our thinking. Since the system pop-up box can't pop up, we won't use the system pop-up box and give him an Activity instead. At this point, however, note that the extra flag ntent.FLAG_ACTIVITY_NEW_TASK should be added when Service is medium or startActivity is in ApplicationContext:


Intent i=new Intent(this,ActDialog.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
// In code ActDialog Actually it is 1 A Activity Apply the theme to it @android:style/Theme.Dialog
// Put activity Make like 1 A Dialog The style of 

This solves the problem that the permission application and the global Dialog of Xiaomi mobile phone cannot be displayed by default.

Finally, in Scheme 1, you can pop up a dialog box without applying for permission, and change the pop-up Window type to LayoutParams.TYPE_TOAST, but this type of pop-up box cannot accept event processing.

Using WindowManager to Implement Global Dialog Box


/**
  *  Show pop-up box 
  *
  * @param context
  */
 public static void showPopupWindow(final Context context, final DialogCallback callback) {
  //  Get WindowManager
  final WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);


  final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
  //  Type 
  params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  //  Settings flag
  params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  //  If you set WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE , pop-up View Not received Back Events of key 
  //  Do not set the transparent mask of this pop-up box to appear black 
  params.format = PixelFormat.TRANSLUCENT;
  // FLAG_NOT_TOUCH_MODAL Do not block event delivery to subsequent windows 
  //  Settings  FLAG_NOT_FOCUSABLE  When the floating window is small, the application icon behind it changes from non-long press to long press 
  //  Do not set this flag If, home There will be problems with the screen drawing of the page 
  params.width = WindowManager.LayoutParams.WRAP_CONTENT;
  params.height = WindowManager.LayoutParams.WRAP_CONTENT;
  params.gravity = Gravity.CENTER;
  TextView textView = new TextView(context);
  textView.setText("sfgsfdsfbsadfbasdfg");
  textView.setTextSize(100);

  final View mView = LayoutInflater.from(context).inflate(R.layout.item_dialog_exit, null);
  TextView tv_itemdialog_title = (TextView) mView.findViewById(R.id.tv_itemdialog_title);
  TextView tv_itemdialog_ok = (TextView) mView.findViewById(R.id.tv_itemdialog_ok);
  TextView tv_itemdialog_close = (TextView) mView.findViewById(R.id.tv_itemdialog_close);

  tv_itemdialog_ok.setText(" Re-login ");
  tv_itemdialog_close.setText(" Log out of the login ");
  tv_itemdialog_title.setText(" This account logs in to another device , If you are not operating , Please change the password in time to prevent information disclosure ");
  tv_itemdialog_ok.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    //  Hide pop-up window 
    mWindowManager.removeView(mView);
    callback.onPositive();
   }
  });

  tv_itemdialog_close.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    mWindowManager.removeView(mView);
    callback.onNegative();
   }
  });

  mWindowManager.addView(textView, params);
 }


Related articles: