0

我一直在使用以下代码每 20 秒设置一次警报

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

}

代码按预期工作,我在 LogCat 中每 20 秒收到一次日志消息。

我想设置一个每日重复的闹钟,在每天上午 10:00 触发。然而,警报并未触发。我在上午 10:00(例如上午 9.59)之前更改了模拟器中的时间,然后运行代码。但是,警报不会在上午 10:00 触发。我还为警报设置了一个ID。我还将模拟器中的日期更改为明天。根本没有发出警报。

为什么会这样?

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

我已经切换回我的旧代码,现在我仍然没有在启动时触发警报。我尝试了以下方法:

  1. 杀死亚行
  2. 重启日食
  3. 新模拟器

这是昨天的 20 秒警报,但现在即使该代码也不起作用。

4

2 回答 2

0

问题不在于代码,而在于模拟器。设备视图下的应用程序未显示该过程。我期待模拟器第一次启动时会触发警报,但是我不得不重新启动模拟器,即从模拟器窗口打开和关闭设备以模仿重新启动。

一旦模拟器“重新启动”,警报就会按预期触发。

于 2013-03-13T14:39:09.543 回答
0

试试下面:

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

我在这里所做的只是添加了 now.setTimeInMillis(System.currentTimeMillis()); 检索当前时间。还要确保您已在清单中添加了接收器。

于 2014-07-31T19:54:05.593 回答