2013-03-13 77 views
0

,我收到每20秒我的日志消息在LogCat中。Android的重复报警管理器不会触发

我想设置一个每日重复闹钟在每天上午10点开火。但是,警报没有被解雇。我在上午10:00之前(例如9.59AM)更改了模拟器中的时间,然后运行代码。然而,闹铃并没有在上午10点发射。我还为闹钟设置了一个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. 重新启动Eclipse
  3. 新模拟器

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

回答

0

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

一旦模拟器“重新启动”,则警报按预期启动。

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的());检索当前时间。 还要确保您已将接收器添加到清单中。