Last active
January 13, 2020 12:09
-
-
Save henieek/395978b8bb975630c0ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.AlarmManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.os.Build.VERSION; | |
public class AlarmManagerCompat { | |
private final AlarmManager mAlarmManager; | |
public AlarmManagerCompat(Context context) { | |
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | |
} | |
@SuppressWarnings("NewApi") | |
public void setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) { | |
if (isAtLeastMarshmallow()) { | |
mAlarmManager.setAndAllowWhileIdle(type, triggerAtMillis, operation); | |
} else { | |
mAlarmManager.set(type, triggerAtMillis, operation); | |
} | |
} | |
@SuppressWarnings("NewApi") | |
public void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) { | |
if (isAtLeastMarshmallow()) { | |
mAlarmManager.setExactAndAllowWhileIdle(type, triggerAtMillis, operation); | |
} else { | |
mAlarmManager.setExact(type, triggerAtMillis, operation); | |
} | |
} | |
private boolean isAtLeastMarshmallow() { | |
return VERSION.SDK_INT >= 23; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment