繁体   English   中英

Android棉花糖中不会弹出“请求权限”对话框

[英]Request permission dialog box is not popping up in Android Marshmallow

我的应用程序需要以下权限:

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

直到棒棒糖运行良好,但从棉花糖开始,我需要在运行时请求许可。 因此,我使用了以下代码:

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

当我尝试运行该应用程序时,不会出现“请求许可权”对话框,并显示一条消息,指出“未授予许可”。 为什么没有显示请求权限对话框?

这是我的build.gradle文件:

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 ,因为它的protectionLevel并不dangerous

通过这种机制无法在运行时请求SYSTEM_ALERT_WINDOW 使用canDrawOverlays()ACTION_MANAGE_OVERLAY_PERMISSION

暂无
暂无

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

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