Issue
Is there a possibility that the below code could fail due to the user denying the SCHEDULE_EXACT_ALARM permission after the permissions check and before the alarm is scheduled?
Note: I have never experienced this issue, this is purely an academic excersize.
public class MyService extends Service {
private void scheduleExactAlarm(Calendar alarmCalendar) {
if(getApplicationContext().checkSelfPermission(Manifest.permission.SCHEDULE_EXACT_ALARM) != PackageManager.PERMISSION_GRANTED) {
// handle permission not granted
return;
}
// can the user deny the permission here?
AlarmManager.AlarmClockInfo alarmInfo = // create alarmInfo with desired information
PendingIntent alarmOperation = // create alarmOperation with desired intent
AlarmManager alarmManager = getSystemService(AlarmManager.class);
alarmManager.setAlarmClock(alarmInfo, alarmOperation);
}
// the rest of the service implementation...
}
EDIT: I learned something new today. Special Permissions are not part of runtime permissions, so using checkSelfPermission(SCHEDULE_EXACT_ALARM)
doesn't make sense. That being said, the question is intended to be broader than the specific permissions/operations used.
Solution
No, it is not possible. Android will terminate your process if Android revokes a permission for any reason (user revoked it in Settings, auto-revocation due to lack of app use on Android 12+, etc.).
That being said, Ian Lake's comment is also valid: checkSelfPermission(Manifest.permission.SCHEDULE_EXACT_ALARM)
should never return is not going to work well. While your concern is valid for PERMISSION_GRANTED
dangerous
permissions, based on current 13 DP1 behavior, SCHEDULE_EXACT_ALARM
is not categorized as dangerous
and is not part of the runtime permission system.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.