Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat Alarm Manager At Exact Interval in API=>19?

I had a massive reading and still I think there is not a clear/complete Answer to this question.

First some stuff to clarify : this question is not concern with battery saving on phone but more about precise timing and I Am a newbie in Android.

Now let me explain the question in more depth. I have a Alarm manager that will invoke a toast (for simplicity) on given interval (every 2 minutes) manager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), interval, pendingIntent); The above will invoke a onReceive() methode on a BroadcastReciver.

public class AlarmReceiver extends BroadcastReceiver  {
@Override
public void onReceive(Context context, Intent intent) {
    //Toast ....bala blah
   }}

Now This was exact interval on API<19 however In API=>19 setRepeating() is not exact anymore ! I found some people suggested (on the other forms) to use setExact(). but there is no example or clear explanation on how to use setExact() in Interval (or I could not find that). As far as I understood setExact() is on off action unlike setRepeating() so according to some others next schedule needs to be set through onReceive() (again I could not find an example ;( . Anyhow this is where I am now . And I really Appreciate any comment or suggestion or link or example ...

I hope I asked my question clear enough, by the way if there is another approach to this (for running a task on precise intervals in API>19 please let me know too) Many thanks

like image 399
bastami82 Avatar asked Feb 02 '15 00:02

bastami82


People also ask

What is an 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 to use 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.

How alarm works in Android?

Basically, an alarm has characteristics which are given below: They fire intents at particular time intervals or at a particular time. They have the ability to run outside the application as well, this enables the alarm to trigger the event even if the application is not running, or the device is asleep.


1 Answers

You can use setExact similarly to setRepeating.

void scheduleAlarm(Context context) {
    AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent yourIntent = new Intent();
    //TODO configure your intent
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, MyApplication.ALARM_REQUEST_CODE, yourIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmMgr.setExact(AlarmManager.RTC_WAKEUP, timeToWakeUp, alarmIntent);
}

The two differences are:

  1. The timing will be exact (or as nearly as possible)
  2. You will have to schedule the next occurrence in the onReceive of your BroadcastReceiver.

      public class AlarmReceiver extends BroadcastReceiver  {
      @Override
      public void onReceive(Context context, Intent intent) {
        //TODO process alarm
        scheduleAlarm(context);
      }}
    
like image 173
Hartok Avatar answered Nov 15 '22 01:11

Hartok