SYSTEM_ALERT_WINDOW-如何在 Android 6.0和 targetSdkVersion 23上自动获得此权限

Facebook,Evernote,Pocket ——所有的应用程序在 Android 6.0上都会自动获得这个许可,即使它们的目标是23(targetSdkVersion=23)。

有很多关于新的棉花糖许可模型的文档。其中之一是 SYSTEM_ALERT_WINDOW被“提升”到“高于危险”级别,因此需要一个特殊的用户干预,以便应用程序被授予这些权限。如果应用程序有 targetSdkVersion22或更低,应用程序将自动获得此权限(如果在清单中请求)。

然而,我注意到一些应用程序获得了这个权限,而不需要将用户发送到 Draw over other apps权限的设置特殊页面。我看到了 Facebook,Evernote,Pocket ——也许还有更多。

任何人都知道如何一个应用程序可以授予这个权限,而无需用户通过 Settings -> Apps -> Draw over other apps

谢谢

101594 次浏览

It is a new behaviour introduced in Marshmallow 6.0.1.

Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store (version 6.0.5 or higher is required), will have granted the permission automatically.

If instead the app is sideloaded, the permission is not automatically granted. You can try to download and install the Evernote APK from apkmirror.com. As you can see you need to manually grant the permission in Settings -> Apps -> Draw over other apps.

These are the commits [1] [2] that allow the Play Store to give the automatic grant of the SYSTEM_ALERT_WINDOW permission.

If the app targets API 22 or lower, then Play Store will give the SYSTEM_ALERT_WINDOW permission and others when the user clicks to install (showing an alert) even if its device is Android 6.0 Otherwise, if the app targets API 23 or above, so that permission will be request to grant in run time.

Yeh After Marshmallow come Android make security level more stick, But For SYSTEM_ALERT_WINDOW you can show floating action and anything You can Force user to give permission for it By Following Codes in your onCreate() method.

Put this code after setContentView:

    // Check if Android M or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Show alert dialog to the user saying a separate permission is needed
// Launch the settings activity if the user prefers
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(myIntent);
}

The action ACTION_MANAGE_OVERLAY_PERMISSION directly launches the 'Draw over other apps' permission screen.


Edit:

My Above Code works 100% Correct

But I just found that many guys are still searching that how can allow ACTION_MANAGE_OVERLAY_PERMISSION permanently like If user has allow Permission Once then don't ask it every time he open application so here is a solution for you:

  1. Check if device has API 23+

  2. if 23+ API then check if user has permit or not

  3. if had permit once don't drive him to Settings.ACTION_MANAGE_OVERLAY_PERMISSION and if has not permit yet then ask for runtime permission check

Put below line in your onCreate() method. Put this after setContentView:

checkPermission();

Now put below code in onActivityResult:

@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);


if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
if (!Settings.canDrawOverlays(this)) {
// You don't have permission
checkPermission();
} else {
// Do as per your logic
}


}


}

Now finally the checkPermission method code:

public void checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
}
}
}

And don't forget to declare this public variable in your class:

public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 5469;

Now(2019) that Google offers an alternative API to SYSTEM_ALERT_WINDOW in the form of Bubbles in Android Q, Google has decided to eventually deprecate SYSTEM_ALERT_WINDOW in a future Android release.

And Android Go devices will no longer grant this permission i.e Settings.canDrawOverlays() == false

For those who want to get this permission automatically when the app is downloaded from the Play Store, besides the SYSTEM_ALERT_WINDOW in the Manifest, you should go to this link and request this from Google.

You have to provide some additional information why you need this permission and Google will review and give you the automatically permission.

Bear in mind that before you ask for this, you have to:

  • Have the SYSTEM_ALERT_WINDOW permission in the Manifest

  • Prompt the user to grant the SYSTEM_ALERT_WINDOW permission within your app, when not already granted

If I miss something feel free to update the answer