简体   繁体   English

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

[英]SYSTEM_ALERT_WINDOW - How to get this permission automatically on Android 6.0 and targetSdkVersion 23

Facebook, Evernote, Pocket - all apps get this permission on Android 6.0 automatically, even though they are targeting 23 ( targetSdkVersion=23 ). Facebook、Evernote、Pocket - 所有应用程序都会在 Android 6.0 上自动获得此权限,即使它们的目标是 23 ( targetSdkVersion=23 )。

There has been a lot of documentation regarding the new Marshmallow permission model.有很多关于新 Marshmallow 权限模型的文档。 One of them is SYSTEM_ALERT_WINDOW been 'promoted' to 'above dangerous' permission class thus requiring a special user intervention in order for apps to be granted with those.其中之一是SYSTEM_ALERT_WINDOW被“提升”为“危险以上”权限级别,因此需要特殊的用户干预才能授予应用程序这些权限。 If the app has targetSdkVersion 22 or lower, app gets this permission automatically (if requested in the manifest).如果应用的targetSdkVersion 22 或更低,应用会自动获得此权限(如果在清单中请求)。

However, I've noticed some apps that get this permission, without needing to send the user to the setting special page of Draw over other apps permission.但是,我注意到一些应用程序获得了此权限,而无需将用户发送到Draw over other apps权限的设置特殊页面。 I saw Facebook, Evernote, Pocket - and perhaps there are more.我看到了 Facebook、Evernote、Pocket——也许还有更多。

Anyone knows how an app can be granted this permission without the user go through Settings -> Apps -> Draw over other apps ?任何人都知道如何在没有用户通过Settings -> Apps -> Draw over other apps情况下授予应用程序此权限?

Thanks谢谢

It is a new behaviour introduced in Marshmallow 6.0.1 .这是 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.每个请求SYSTEM_ALERT_WINDOW权限并通过 Play 商店(需要6.0.5或更高版本)安装的应用程序都会自动授予权限。

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 .您可以尝试从apkmirror.com下载并安装 Evernote APK。 As you can see you need to manually grant the permission in Settings -> Apps -> Draw over other apps .如您所见,您需要在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.这些是允许 Play 商店自动授予SYSTEM_ALERT_WINDOW权限的提交[1] [2]

Yeh After Marshmallow come Android make security level more stick, But For是的,在 Marshmallow 之后,Android 使安全级别更加稳定,但是对于

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您可以显示浮动操作和任何内容您可以通过在您的onCreate()方法中遵循代码来强制用户授予权限 将此代码放在 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.操作ACTION_MANAGE_OVERLAY_PERMISSION直接启动“绘制其他应用程序”权限屏幕。


Edit: My Above Code works 100% Correct编辑:我上面的代码工作 100% 正确

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 hear solution for you-但我发现很多人仍在寻找如何永久允许ACTION_MANAGE_OVERLAY_PERMISSION就像如果用户有允许权限一次然后不要每次他打开应用程序时都问它,所以听听你的解决方案 -

  1. Check if device has API 23+检查设备是否有 API 23+

  2. if 23+ API then check if user has permit or not如果 23+ API 然后检查用户是否有许可

  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如果曾经有过许可,请不要将他带到Settings.ACTION_MANAGE_OVERLAY_PERMISSION ,如果还没有许可,则要求进行运行时权限检查

Put below line in your onCreate() method.在您的onCreate()方法中放置以下行。 Put this after setContentView把这个放在setContentView之后

checkPermission();

Now put below code in onActivityResult现在把下面的代码放在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现在终于是 checkPermission 方法代码

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.现在( 2019 年)谷歌在 Android Q 中以气泡的形式提供了SYSTEM_ALERT_WINDOW的替代 API,谷歌决定最终在未来的 Android 版本中弃用SYSTEM_ALERT_WINDOW

And Android Go devices will no longer grant this permission ie Settings.canDrawOverlays() == false Android Go 设备将不再授予此权限,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.对于那些希望在从 Play 商店下载应用程序时自动获得此权限的人,除了 Manifest 中的 SYSTEM_ALERT_WINDOW 之外,您应该转到此链接并向 Google 请求此权限。

You have to provide some additional information why you need this permission and Google will review and give you the automatically permission.您必须提供一些额外信息,为什么需要此权限,Google 会审核并自动授予您权限。

Bear in mind that before you ask for this, you have to:请记住,在您提出要求之前,您必须:

  • Have the SYSTEM_ALERT_WINDOW permission in the Manifest在 Manifest 中拥有 SYSTEM_ALERT_WINDOW 权限

  • Prompt the user to grant the SYSTEM_ALERT_WINDOW permission within your app, when not already granted提示用户在您的应用程序中授予 SYSTEM_ALERT_WINDOW 权限(如果尚未授予)

If I miss something feel free to update the answer如果我错过了什么,请随时更新答案

如果应用面向 API 22 或更低版本,则即使其设备是 Android 6.0,Play 商店也会在用户点击安装时授予 SYSTEM_ALERT_WINDOW 权限和其他权限(显示警报)否则,如果应用面向 API 23 或更高版本,则将请求在运行时授予权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM