On the pit of pop-up dialog in Service

Keywords: Android Mobile xml Windows

1, Mobile phone version problem, most articles do not cover this point, resulting in their code can not be used normally

Type > Application > overlay required for version M and above

AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext()); 
builder.setTitle(
"Tips");
builder.setMessage(
"service Elastic frame");
builder.setNegativeButton(
"Got it",null);
Dialog dialog
=builder.create();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){//6.0       
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);        
}else
{       
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);        
}

dialog.show();

At the same time, because it involves different versions of Android system

The declaration in Android manifest.xml requires permission corresponding to these two types at the same time

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

 

 

2, Permission problem. The pop-up dialog box in service is the system pop-up box. You need to apply for suspension window permission above version M

Before Android 6.0, floating windows were allowed by default

Note the use of Android after 6.0

Because the system alert window permission is a special permission, you need to apply for it separately

Special permissions, as the name suggests, are special sensitive permissions. In Android system, there are two main permissions

System? Alert? Window
 Write? Settings
 For the authorization of the above two special permissions, use startActivityForResult to start the authorization interface.

 

Application method:

private static final int REQUEST_CODE = 1;
private  void requestAlertWindowPermission() {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 if (Settings.canDrawOverlays(this)) {
    Log.i("xqxinfo", "onActivityResult granted");
     }
   }
  }
}

Posted by nagalenoj on Thu, 09 Apr 2020 08:06:19 -0700