简体   繁体   中英

Android AlarmManager send Broadcast not on time

I use AlarmManager to set two action on time,

First action set on 10:00:00, and second action set on 10:15:00.

I can get two action broadcast,

and get first action broadcast on 10:00:03 (is OK),

but get second action broadcast on 10:29:15,14 minutes late!

How let AlarmManager can send broadcast on time ??

Set AlarmManager code:

@Override
public void onCreate() {
    setSchedule();
}
private void setSchedule(){
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Calendar offCal = Calendar.getInstance();
    offCal.set(Calendar.HOUR_OF_DAY, 10);
    offCal.set(Calendar.MINUTE, 15);
    offCal.set(Calendar.SECOND, 00);
    Intent offIntent = new Intent(this, AlarmReceiver.class);
    offIntent.setAction(AlarmReceiver.ALUM_SCREEN_OFF);
    PendingIntent offPending = PendingIntent.getBroadcast(this, 1,
            offIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC, offCal.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, offPending);

    Intent onIntent = new Intent(this, AlarmReceiver.class);
    onIntent.setAction(AlarmReceiver.ALUM_SCREEN_ON);
    Calendar onCal = Calendar.getInstance();
    onCal.set(Calendar.HOUR_OF_DAY, 10);
    onCal.set(Calendar.MINUTE, 00);
    onCal.set(Calendar.SECOND, 00);
    PendingIntent onPending = PendingIntent.getBroadcast(this, 2,
            onIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, onCal.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, onPending);
}

Receiver code:

public class AlarmReceiver extends BroadcastReceiver {
    public static final String ALUM_SCREEN_ON = "screenOn";
    public static final String ALUM_SCREEN_OFF = "screenOff";
    private static final String TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.d(TAG, "get braodcast action:"+intent.getAction());
}

Hi you are using setRepeating method which wont give alarm at exact time.Alarm manager will strongly discourage other apps apart from system apps to trigger alarm accurately for the better battery optimization.so if u want to trigger alarm at a particular time means you can use setExact() method This also wont give you complete gaurantee but it will give you the most accuracy one .While setting multiple alarms at a single time make sure you are giving different ids in `

PendingIntent onPending = PendingIntent.getBroadcast(this, 2,
            onIntent, PendingIntent.FLAG_UPDATE_CURRENT);

here you gave 2 as id and it should be different for different alarms.

Since KitKat the set and setRepeating APIs are not exact. If you absolutely need the alarm to run at a specific time, use setExact. Consider using setWindow instead if the alarm time is not critical.

There is no setExactRepeating, so if you need that, you need to use setExact and them re-arm your alarm. But unless your are doing time critical stuff, that's not recommended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM