2013-03-13 75 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的());檢索當前時間。 還要確保您已將接收器添加到清單中。