简体   繁体   中英

Android Repeating Alarm Manager not Fired

I have been using the following code to set fire an alarm every 20 seconds

public class SchedulerSetupReceiver extends BroadcastReceiver {
private static final String APP_TAG = "LOG: ";


private static final int EXEC_INTERVAL = 20 * 1000;

@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(ctx, SchedulerEventReceiver.class); 
    PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar now = Calendar.getInstance();
    now.add(Calendar.SECOND, 20);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);
}

}

The code works as expected and I receive my log message every 20 seconds in LogCat.

I would like to set a daily repeating alarm to fire at 10:00AM every day. However the alarm is not fired. I have changed the time in my emulator before 10:00AM (eg 9.59AM) and then run the code. However the alarm is not fired at 10:00AM. I have also set an id for the alarm. I have also changed the date in the emulator to tomorrow. No alarms are fired at all.

Any reasons as to why this is the case?

public class SchedulerSetupReceiver extends BroadcastReceiver {
private static final String APP_TAG = "LOG: ";
private static final int ALARM_ID_2000 = 2000;

//private static final int EXEC_INTERVAL = 20 * 1000;

@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(ctx, SchedulerEventReceiver.class);       
    PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, ALARM_ID_2000, i,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(System.currentTimeMillis());

    Calendar updateTime = new GregorianCalendar();
    //we want to set a daily alarm at 10:00AM
    updateTime.add(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR));
    updateTime.set(Calendar.HOUR_OF_DAY,10);
    updateTime.set(Calendar.MINUTE,0);
    updateTime.set(Calendar.SECOND,0);
    updateTime.set(Calendar.MILLISECOND,0);
    updateTime.set(Calendar.DATE,cal.get(Calendar.DATE));
    updateTime.set(Calendar.MONTH,cal.get(Calendar.MONTH));

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentExecuted);
}

I have switched back to my old code and now im still not getting the alarms fired on boot. I have tried the following:

  1. Kill ADB
  2. Restart eclipse
  3. New emulator

This was working with the 20 second alarm yesterday, but now even that code doesn't work.

The issue was not with the code but the emulator. The process was not showing for the app under the devices view. I was expecting the alarms to fire when the emulator first starts, however I had to reboot the emulator ie turning the device on and off from the emulator window in order to mimick a reboot.

Once the emulator "rebooted" then the alarms fire as expected.

Try below:

public class SchedulerSetupReceiver extends BroadcastReceiver {
private static final String APP_TAG = "LOG: ";
private static final int EXEC_INTERVAL = 20 * 1000;

@Override
public void onReceive(final Context ctx, final Intent intent) {
Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
AlarmManager alarmManager = (AlarmManager) ctx
        .getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctx, SchedulerEventReceiver.class); 
PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
        PendingIntent.FLAG_CANCEL_CURRENT);
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);
}
}

All I have changed here is I have added now.setTimeInMillis(System.currentTimeMillis()); to retrieve current time. Also ensure you have added the receiver in manifest.

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