简体   繁体   中英

How to access SYSTEM_ALERT_WINDOW permission run time?

This is my code for reference. I have added the run time permission code and system granting the permission but again it gives an error:

Failed due to non declaration of following android.permission.SYSTEM_ALERT_WINDOW permission in Manifest file

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();
            }
        }
    }
}

Manifest File :

<?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();
                }
            }
        }
    }

Please try this way. May be you are facing condition problem,

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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