Reputation: 51
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'
}
Upvotes: 2
Views: 851
Reputation: 1007320
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
.
Upvotes: 2