Reputation: 107
I have app which set alarms. I use this code to ask user for permission:
Intent intent = new Intent(
"android.settings.REQUEST_SCHEDULE_EXACT_ALARM",
Uri.parse("package:"+ getPackageName())
);
someActivityResultLauncher.launch(intent);
In manifest I added permission:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
When I click the button which launch the intent I can see:
But I can't toggle button with permission and I don't know why.
Upvotes: 1
Views: 1934
Reputation: 1
// 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) {
}
});
Upvotes: -1
Reputation: 76789
It will return isGranted = true
when the permission is being requested the AndroidManifest.xml
.
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
And actually, the intent is 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())
));
}
}
Listen for broadcast action AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED
.
And there's also permission USE_EXACT_ALARM
, when targeting API 33
onwards.
See behavioral changes: exact alarm permission.
Upvotes: 2