我有设置闹钟的应用程序。我使用此代码来请求用户许可:
Intent intent = new Intent(
"android.settings.REQUEST_SCHEDULE_EXACT_ALARM",
Uri.parse("package:"+ getPackageName())
);
someActivityResultLauncher.launch(intent);
在清单中我添加了权限:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
当我单击启动意图的按钮时,我可以看到:
但是我无法在获得许可的情况下切换按钮,我不知道为什么。
当请求权限时,它将返回
isGranted = true
AndroidManifest.xml
。
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
实际上,目的是
Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
:
ActivityResultLauncher<Intent> startActivityForResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
if (result.getResultCode() == RESULT_OK) {}
});
@RequiresApi(api = Build.VERSION_CODES.S_V2)
void hasPermission(@NonNull Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
// it depends on the manifest permission, just alike the toggle does.
if (! alarmManager.canScheduleExactAlarms()) {
}
// one can only disable the permission, when the manifest requested it.
startActivityForResult.launch(new Intent(
Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
Uri.parse("package:" + getPackageName())
));
}
}
AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED
。
当目标 API USE_EXACT_ALARM
以后,还有权限
33
。
查看行为变化:确切的警报权限。
// Button click for check permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
hasPermission(AlarmActivity.this);
}
// 2 Number code
@RequiresApi(api = Build.VERSION_CODES.S_V2)
void hasPermission(@NonNull Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
// it depends on the manifest permission, just alike the toggle does.
if (!alarmManager.canScheduleExactAlarms()) {
startActivityForResult.launch(new Intent(
Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
Uri.parse("package:" + getPackageName())
));
}
// one can only disable the permission, when the manifest requested it.
}
}
// 3 Number code
ActivityResultLauncher<Intent> startActivityForResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
if (result.getResultCode() == RESULT_OK) {
}
});