How to turn off overlay permission on android

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 03:56:01

问题


https://facebook.github.io/react-native/docs/integration-with-existing-apps.html

https://medium.com/react-native-development/fixing-problems-in-react-native-caused-by-new-permission-model-on-android-1e547f754b8

talks about overlay permission.

They add the permission check code to the activity which hosts react native view.

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, OVERLAY_PERMISSION_REQ_CODE);
    }
}

It seems the permission is required because react native needs to show the debug window in an overlay.

How to turn it off in production build?


回答1:


I turned it off in release with two changes, one in the manifest, and one in the code.

First: In your main Manifest (src/main/AndroidManifest.xml), remove the line:

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

Add a new manifest in src/debug/AndroidManifest.xml that looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.mycompany.myapp">

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

This will merge the permission only on debug variants.

Second, to the code you mentioned, I added a check for BuildConfig.DEBUG:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.canDrawOverlays(this) && BuildConfig.DEBUG) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
    }
}


来源:https://stackoverflow.com/questions/45347562/how-to-turn-off-overlay-permission-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!