Use AlarmManagerCompat.

Fixes #12468
This commit is contained in:
Isira Seneviratne 2022-09-23 07:26:58 +05:30 committed by Cody Henthorne
parent 1b63ed0b20
commit e67ac95890

View file

@ -6,9 +6,11 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.SystemClock;
import androidx.core.app.AlarmManagerCompat;
import androidx.core.content.ContextCompat;
import org.signal.core.util.logging.Log;
import org.whispersystems.signalservice.api.util.SleepTimer;
@ -66,23 +68,14 @@ public class AlarmSleepTimer implements SleepTimer {
private void setAlarm(long millis, String action) {
final Intent intent = new Intent(action);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
final AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
final AlarmManager alarmManager = ContextCompat.getSystemService(context, AlarmManager.class);
Log.w(TAG, "Setting alarm to wake up in " + millis + "ms.");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + millis,
pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + millis,
pendingIntent);
} else {
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + millis,
pendingIntent);
}
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager,
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + millis,
pendingIntent);
}
@Override