简体   繁体   中英

Request permission dialog box is not popping up in Android Marshmallow

My app requires the following permissions:

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

Until lollipop it was working fine but starting with Marshmallow, I need to ask for permission at runtime. So, I used this code:

if (ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.SYSTEM_ALERT_WINDOW)==PackageManager.PERMISSION_GRANTED){
    //my code here   
} else {
    if (shouldShowRequestPermissionRationale(Manifest.permission.SYSTEM_ALERT_WINDOW)) {
        Toast.makeText(getApplicationContext(),"Permission is requird",Toast.LENGTH_SHORT).show();
    }
    requestPermissions(new String[] {Manifest.permission.SYSTEM_ALERT_WINDOW},REQUEST_RESULT);
}


@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResult){
    if (requestCode==REQUEST_RESULT){
        if (grantResult[0]==PackageManager.PERMISSION_GRANTED){
            //my code here
        }else {
            Toast.makeText(getApplicationContext(),"permission has not granted",Toast.LENGTH_SHORT).show();
        }
    }
    else {
        super.onRequestPermissionsResult(requestCode,permissions,grantResult);
    }
}

When I try to run the app, the request permission dialog box does not appear and display a message saying "permission has not been granted". Why is the request permission dialog box is not appearing?

Here is my build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "com.example.adarsh.ezswipe"
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
}

KILL_BACKGROUND_PROCESSES does not need to be requested at runtime, as its protectionLevel is not dangerous .

SYSTEM_ALERT_WINDOW cannot be requested at runtime through this mechanism. Please use canDrawOverlays() and ACTION_MANAGE_OVERLAY_PERMISSION .

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