|
|
|
@ -33,11 +33,7 @@ public class AlarmManagerCompatEx {
|
|
|
|
|
static void setAndAllowWhileIdle(
|
|
|
|
|
@NonNull Context context, @NonNull AlarmManager am,
|
|
|
|
|
int type, long trigger, @NonNull PendingIntent pi) {
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
boolean exact_alarms = prefs.getBoolean("exact_alarms", true);
|
|
|
|
|
|
|
|
|
|
// https://developer.android.com/about/versions/12/behavior-changes-12#exact-alarm-permission
|
|
|
|
|
if (exact_alarms && canScheduleExactAlarms(context))
|
|
|
|
|
if (hasExactAlarms(context))
|
|
|
|
|
try {
|
|
|
|
|
AlarmManagerCompat.setExactAndAllowWhileIdle(am, type, trigger, pi);
|
|
|
|
|
} catch (SecurityException ex) {
|
|
|
|
@ -48,10 +44,17 @@ public class AlarmManagerCompatEx {
|
|
|
|
|
AlarmManagerCompat.setAndAllowWhileIdle(am, type, trigger, pi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boolean hasExactAlarms(Context context){
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
boolean exact_alarms = prefs.getBoolean("exact_alarms", true);
|
|
|
|
|
return (exact_alarms && canScheduleExactAlarms(context));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boolean canScheduleExactAlarms(Context context) {
|
|
|
|
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R)
|
|
|
|
|
return true;
|
|
|
|
|
else {
|
|
|
|
|
// https://developer.android.com/about/versions/12/behavior-changes-12#exact-alarm-permission
|
|
|
|
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
return am.canScheduleExactAlarms();
|
|
|
|
|
}
|
|
|
|
|