Alarm Manager not working at specific given time i

2019-05-01 07:49发布

问题:

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.
  }

回答1:

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



回答2:

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.