ActivityCompat.requestPermissions 未调用
ActivityCompat.requestPermissions not invoked
我正在使用以下代码检查权限。我总是到达 shouldShowRequestPermissionRationale 为真的部分。然后显示Toast但不显示权限对话框
// 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);
}
}
这是我的应用程序模块:
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'
}
在清单中,我已经声明了权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
该应用程序在 Lollipop 设备上运行良好。
我正在请求更改系统亮度的权限。
我找到了答案。我们需要请求许可以不同的方式更改设置。我们需要通过 intent 打开一个 activity,然后用户将从那里允许我们的应用程序。这是代码:
// checking permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(this)) {
Toast.makeText(this, "Please allow the app to change the settings", Toast.LENGTH_LONG).show();
Intent intentt = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intentt.setData(Uri.parse("package:" + this.getPackageName()));
intentt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentt);
} else {
changeBrightness(extras); // it's android M and we have permission. Do permission related work here.
}
} else {
changeBrightness(extras); // It's pre android M and we have got permission at install time. Do permission related work here
}
我正在使用以下代码检查权限。我总是到达 shouldShowRequestPermissionRationale 为真的部分。然后显示Toast但不显示权限对话框
// 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);
}
}
这是我的应用程序模块:
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'
}
在清单中,我已经声明了权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
该应用程序在 Lollipop 设备上运行良好。 我正在请求更改系统亮度的权限。
我找到了答案。我们需要请求许可以不同的方式更改设置。我们需要通过 intent 打开一个 activity,然后用户将从那里允许我们的应用程序。这是代码:
// checking permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(this)) {
Toast.makeText(this, "Please allow the app to change the settings", Toast.LENGTH_LONG).show();
Intent intentt = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intentt.setData(Uri.parse("package:" + this.getPackageName()));
intentt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentt);
} else {
changeBrightness(extras); // it's android M and we have permission. Do permission related work here.
}
} else {
changeBrightness(extras); // It's pre android M and we have got permission at install time. Do permission related work here
}