5

Hi I am using alarm Manager for specific time interval for 3 minutes and I started monitoring. It worked for sometimes and suddenly I noticed there is irregular time interval which is not correct! You can see in attached log where at "20-Jul-2016 12:22:03 pm" time varies! I connected the phone and turned off the screen and monitored! where for every 3 minutes, i hit the server and gets the response as 1. But at one time, it takes 5 minutes to hit the server! Why this strange issue happened?

Here is code.

 public void startAt3() {
    Intent alarmIntent = new Intent(ActivityTracking.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(ActivityTracking.this, 0, alarmIntent, 0);
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        /* Set the alarm */
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    /* Repeating on every 3 minute interval */
    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(),
            180000L, pendingIntent);
    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    Log.e("alarm",mydate);

}

AlarmReceiver:

   public class AlarmReceiver extends WakefulBroadcastReceiver    {//AlarmReceiver class
@Override
public void onReceive(Context context, Intent intent) {//onReceive method
    String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
    Log.e("alarm",mydate);
    Intent service = new Intent(context, SimpleWakefulService.class);//intent to call another class
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, service);//service started
}

SimpleWakefulService:

    public class SimpleWakefulService extends IntentService {
      public SimpleWakefulService() {
        super("SimpleWakefulService");//instantiates simpleWakefulService

    }
     @Override
     protected void onHandleIntent(Intent intent) {
        Log.e("simpleWakeful","simpleWakeful");
      serviceCall(this); //here is downloadTaskMethod called and getting response as 1.
  }

enter image description here

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Shadow
  • 6,864
  • 6
  • 44
  • 93

2 Answers2

6

Don't use setInexactRepeating. This, as the name suggests, doesn't schedule the alarm to go off at an exact time.

You can use something like this:

public void scheduleSingleAlarm(Context context) {
    Intent intent = new Intent(context, NotificationReceiver.class);
    PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(context,
            SINGLE_ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    Calendar futureDate = Calendar.getInstance();
    futureDate.add(Calendar.MINUTE, 3);

    setSingleExactAlarm(futureDate.getTime().getTime(), pendingUpdateIntent);
}

@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);
    }
}

When you receive a call from the alarm, schedule another alarm to go off in another three minutes. I've blogged about this here

Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
  • thanks for your reply. I have one doubt from blog. in onReceive method, what is WrappedAlarmManager? what I need to use in my code? – Shadow Jul 20 '16 at 07:54
  • also interval_seven seconds you have mentioned right. what i should give? – Shadow Jul 20 '16 at 08:05
  • The code is available on my Github account here: https://github.com/hoombar/android-training/tree/master/src/net/rdyonline/android_training/alarms – Ben Pearson Jul 20 '16 at 08:11
  • No problem. I've updated the post as well to link through to the code directly, thanks. – Ben Pearson Jul 20 '16 at 08:15
  • sir, i have a questions, is the code above will repeated everyday? Because i need to create an alarm that execute a task repeated everyday. – Sen Mar 16 '17 at 04:03
0

You can see from the official documentation for AlarmManager that starting from Android version 19 (KitKat), alarms scheduled will be inexact even if they were marked exact (although you already have inexactRepeating()).

The alarms will get bunched with system events to minimize device wake up and battery use which is what you are seeing. This is by design.

ucsunil
  • 7,378
  • 1
  • 27
  • 32