我的应用需要以下权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
直到棒棒糖它工作正常但从Marshmallow开始,我需要在运行时请求许可。所以,我使用了这段代码:
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'
}
答案 0 :(得分:2)
KILL_BACKGROUND_PROCESSES
不需要在运行时请求,因为protectionLevel
不是dangerous
。
SYSTEM_ALERT_WINDOW
。请use canDrawOverlays()
和ACTION_MANAGE_OVERLAY_PERMISSION
。