2011-04-11 56 views
4

我已設置鬧鐘,以在Android設備處於開機狀態時提醒我。但是,當我關閉設備,並再次提醒警報不工作。你們可以請我建議我怎麼能解決這個問題?當設備關閉並再次打開時,鬧鐘在android中不起作用

我的代碼看起來像這樣,

Intent myIntent = new Intent(getApplicationContext(), serviceclass.class); 
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 
    CONST+id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

Calendar calender = Calendar.getInstance(); 
calender.setTimeInMillis(System.currentTimeMillis()); 
calender.set(Calendar.HOUR_OF_DAY, hours); 
calender.set(Calendar.MINUTE, ireminder.getMin()); 
calender.set(Calendar.SECOND, 0); 
calender.set(Calendar.MILLISECOND, 0); 
calender.set(Calendar.DAY_OF_WEEK, day); 

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 

calender.getTimeInMillis(), 7 * AlarmManager.INTERVAL_DAY, pendingIntent);     

回答

4

警報將在重新啓動被清除。

你可以做的是

  1. 堅持一個數據庫表報警信息的REBOOT_COMPLETED - 活動
  2. 在重新啓動
  3. 寄存器,啓動一個後臺線程重新註冊報警。確保您正確計算鬧鐘時間。

請參閱API:「註冊的警報在設備睡着時保留(如果在此期間關閉設備,可以選擇喚醒設備),但是如果設備關閉並重新啓動,則會被清除。 - http://developer.android.com/reference/android/app/AlarmManager.html

1

創建內部清單

<receiver android:name=".OnBootReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

類OnBootReceiver

public class OnBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     mgr.setRepeating(AlarmManager.RTC_WAKEUP, 
     SystemClock.currentThreadTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, 
        pi); 

    } 

} 

您的活動中

sendBroadcast(new Intent(this, OnBootReceiver.class));