Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set exact, repeating alarms in Android 4.4?

Right now, I am setting alarms like this:

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        timedate = cal.getTime();

        //convert date to milis
        long timeInMilis = (timedate.getTime());

        //Set Alarm to Repeat
        manager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMilis, interval, pendingIntent);

Unfortunately, the scheduled times for repeating are inexact and I read that they could be inexact for a full interval!

So, I would like to switch over to seting an exact repeating alarm. My device requires a minimum only of the latest API 19/Android 4.4, so I can't use setRepeating.

What can I do instead to set an exact repeating alarm?

like image 450
Kala J Avatar asked Nov 16 '14 16:11

Kala J


People also ask

How do you set a repeat alarm on Android?

At the top of the main panel you should see an option to Add alarm. Tap this and you'll be presented with a time in the top half of the screen, with various settings in the lower half. Scroll the hours up or down until you reach the one you want, then repeat the process with the minutes.

How do I set multiple alarms on Android?

If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntent s with different requestCode . If requestCode is the same, then the new alarm will overwrite the old one.

What is exact alarm?

The new exact alarm permission ( SCHEDULE_EXACT_ALARM ) was created to save system resources. Alarms that must be executed in an exact time may be triggered when the phone is on power-saving mode or Doze, making the app consumes more battery than it should.

How do I use Android alarm Manager?

This example demonstrates how do I use AlarmManager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

Unfortunately, the scheduled times for repeating are inexact and I read that they could be inexact for a full interval!

If your targetSdkVersion is 19 or higher, yes.

What can I do instead to set an exact repeating alarm?

Use setExact() to get control for your initial delay. Then, as part of your work for processing that event, use setExact() to get control at your next desired time. IOW, you do the "repeating" part yourself.

Yes, this is irritating.

It is intentionally irritating, to steer developers to not use exact repeating alarms, as those are worse for the battery than are their inexact brethren. If the user will perceive the inexact behavior and will not appreciate it, feel free to use setExact() to make the alarms occur when the user wants. However, if the user will not perceive the inexact behavior, please use inexact alarms, as the user may notice the battery impact of your exact alarms.

like image 191
CommonsWare Avatar answered Oct 16 '22 17:10

CommonsWare