Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager For API 17 & 19

Tags:

android

api

I have the following code which will call my intend 'Alarm Receiver' every hour to perform a check

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent broadcast = PendingIntent.getBroadcast(context,100,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR,1);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

And this works great for Android API 19+ (Android 4.4)

However I want to allow Android API 17 (4.2) to be able to use my app as not everyone can get 4.4 on their phones

The problem is the setExact which is not supported by API 17

Is there a way of calling the Intent to be called which is supported by API 17+ or would I have to somehow find out what verison of android is running and perform one piece of code or another?

Thanks

like image 466
Sjharrison Avatar asked Oct 22 '15 08:10

Sjharrison


People also ask

Is AlarmManager deprecated?

IntentService + WakefulBroadcastReceiver + AlarmManager are deprecated with API 26 (Android 8.0 Oreo).

How is AlarmManager different from WorkManager?

Unlike WorkManager, AlarmManager wakes a device from Doze mode. It is therefore not efficient in terms of power and resource management.

What is exact alarm API?

These kinds of alarms — called exact alarms — have the ability to bring the device out of “Doze mode,” one of Android's core battery-saving restrictions.


2 Answers

The way that AlarmManager works was changed in API level 19 to help save battery life. It was so that events could be batched. What this means is that set() and setRepeating() will be inexact on API 19+.

In API versions before 19, you can just use set, which will schedule an alarm for the exact time.

Unfortunately this means that you'll need to add a check in your code to make sure that you use the correct method for setting an exact alarm:

@SuppressLint("NewApi")
private void setSingleExactAlarm(long time, PendingIntent pIntent) {
    if (android.os.Build.VERSION.SDK_INT >= 19) {
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
    } else {
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);
    }
}

Additionally, it would be worth reading about Doze that was introduced in Marshmallow and decide how you want your app to work alongside it.

like image 175
Ben Pearson Avatar answered Sep 21 '22 03:09

Ben Pearson


If you want to trigger an exact alarm you should use something like this:

if (android.os.Build.VERSION.SDK_INT >= 19) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, NEXT_TIME_IN_MILLISECONDS, operation);
} else {
    alarmManager.set(AlarmManager.RTC_WAKEUP, NEXT_TIME_IN_MILLISECONDS, operation);
}

Take into account things have changed in Android 6 with doze feature so feel free to check this documentation

like image 26
iGoDa Avatar answered Sep 20 '22 03:09

iGoDa