Ich überprüfe die Berechtigung mit dem folgenden Code. Ich erreiche immer den Teil, wo sollteShowRequestPermissionRationale wahr ist. Dann wird der Toast angezeigt, aber der Erlaubnisdialog wird nicht angezeigt.ActivityCompat.requestPermissions wird nicht aufgerufen
// checking permission
if (ContextCompat.checkSelfPermission(this,
"android.permission.WRITE_SETTINGS")
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
"android.permission.WRITE_SETTINGS")) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Toast.makeText(this, "Please allow the app to change the settings otherwise the app cannot change the brightness for you", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(this,
new String[]{"android.permission.WRITE_SETTINGS"},
0);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{"android.permission.WRITE_SETTINGS"},
0);
}
}
Hier ist meine App-Modul:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.appsbyusman.brightnesscontrol"
minSdkVersion 15
targetSdkVersion 23
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:23.3.0'
compile 'com.android.support:design:23.3.0'
}
In Manifest, ich habe die Erlaubnis erklärt:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Die App arbeitet in Lollipop Gerät in Ordnung. Ich bitte um Erlaubnis, die Systemhelligkeit zu ändern.