AlarmManager triggers PendingIntent too soon for t

2019-07-15 05:18发布

问题:

I have this code:

set alarm:

public void setAlarm()
{
    Calendar Calendar_Object = Calendar.getInstance();

    Calendar_Object.add (Calendar.DAY_OF_YEAR, 1);

    Calendar_Object.set(Calendar.HOUR_OF_DAY, 0);
    Calendar_Object.set(Calendar.MINUTE, 0);
    Calendar_Object.set(Calendar.SECOND, 1);

    Intent myIntent = new Intent(Main.this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(Main.this,
            0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),1000*60*60*24, pendingIntent);
}

Broadcast receiver:

public class AlarmReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {

      Intent myIntent = new Intent(context, NotificationService.class);
      context.startService(myIntent);
    }

} 

the service:

public class NotificationService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

}

@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService( this.getApplicationContext().NOTIFICATION_SERVICE);

    Intent intent1 = new Intent(this.getApplicationContext(), ABC.class);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "xxx", System.currentTimeMillis());

    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(this.getApplicationContext(),
            "abc", "xyz",
            pendingNotificationIntent);

    mManager.notify(0, notification);
    }
}

after setting the alarm at 00:00:01 everything is perfect but the next time the problem is happening. the PendingIntent triggers like 6 to 8 times between the 24 hours interval. I don't remember if the time is the same for each trigger but I want it one time each day. What is wrong with the code?

回答1:

This will create an alarm that will go off every day at 00:05, the important part is the AlarmManager.INTERVAL_DAY

Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 5);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.HOUR, 24);

PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
mgr.cancel(pi);
mgr.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

Also, don't forget to always cancel previous similar alarms, when re-setting a new one, or else they'll all go off, old set alarms, and the new one, see above mgr.cancel(pi);