如何访问SYSTEM_ALERT_WINDOW权限运行时?

时间:2017-07-12 10:55:04

标签: android runtime-permissions

这是我的代码供参考。我已经添加了运行时权限代码和系统授予权限,但它再次给出了错误:

由于未在Manifest文件中声明以下android.permission.SYSTEM_ALERT_WINDOW权限而失败

public boolean checkDrawOverlayPermission()
{
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
    {
        return true;
    }
    if (!Settings.canDrawOverlays(this))
    {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, REQUEST_CODE);
        return false;
    }
    else
    {
        return true;
    }
}

@Override
@TargetApi(Build.VERSION_CODES.M)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE)
    {
        if (Settings.canDrawOverlays(this))
        {
            //startService(new Intent(this, PowerButtonService.class));
            //Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show();

            if(!checkPermission())
            {
                requestPermission();
            }
            else
            {
                //Toast.makeText(this, "Phone State Permission Already Granted", Toast.LENGTH_SHORT).show();
                //callBeacon();
            }
        }
    }
}

清单文件:

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

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
    <!-- Declaration that this app is usable on phones with Bluetooth Low Energy. -->
    <uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="true" />
    <!-- Declaration permission for using internet -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />
        <!-- <activity android:name="com.facebook.LoginActivity" /> -->
        <activity
            android:name="com.beaconstreamsdk.com.MainActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" />

        <service
            android:name="com.beaconstreamsdk.com.service.BeaconService"
            android:enabled="true"
            android:exported="false" />
        <service
            android:name="com.beaconstreamsdk.com.service.FloatingHeadService"
            android:enabled="true"
            android:exported="false" />
        <service
            android:name="com.beaconstreamsdk.com.service.ConnectionService"
            android:enabled="true"
            android:exported="false" />

        <activity
            android:name="com.beaconstreamsdk.com.SignUpActivity"
            android:screenOrientation="portrait" />
        <activity android:name=".Main2Activity" />
        <activity android:name=".BeaconStreamActivity"></activity>
    </application>

</manifest>

onActivityResult:

@Override
    @TargetApi(Build.VERSION_CODES.M)
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_CODE)
        {
            if (Settings.canDrawOverlays(this))
            {
                //startService(new Intent(this, PowerButtonService.class));
                //Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show();

                if(!checkPermission())
                {
                    requestPermission();
                }
                else
                {
                    Toast.makeText(this, "Phone State Permission Already Granted", Toast.LENGTH_SHORT).show();
                    //callBeacon();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

请尝试这种方式。可能是你正面临着条件问题,

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, REQUEST_CODE);
        return false;
    }
    else
    {
        return true;
    }
    }else{
        return true;
}