我一直在搜索這個網站,並找到一些與設置鬧鐘有關的答案。我已經成功地設置了鬧鐘。如何設置即使重新啓動我的android手機也會關閉的鬧鐘?
我要做的就是:
- 從我設置鬧鐘,在特定的時間和日期將調用接收器
- 從接收我所說的服務
- 從我送服務活動(在通知欄上)給用戶的通知。
我的問題是:
我建立了一個警報從現在起5分鐘。假設我關掉手機並重新打開(似乎忘記了鬧鐘)。我怎樣才能防止這種情況發生?
我真的需要調用一個服務來發送通知嗎,或者我可以從接收端進行嗎?
下面是關於前面的部分(a)中所引用的代碼:
Intent intent = new Intent(MyActivity.this,
AlarmReceiver.class);
intent.putExtra("alarm_message", "Something");
PendingIntent mAlarmSender;
mAlarmSender = PendingIntent.getBroadcast(
MyActivity.this, 0, intent, 0);
// We want the alarm to go off 30 seconds from now.
long alarmTime = dateMgmt.getTimeForAlarm(pickedDate);
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmTime + 15000,
mAlarmSender);
這是上一節(b)中所引用的代碼:
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, MyService.class);
context.startService(newIntent);
} catch (Exception e) {
Toast
.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
此代碼中引用上一節(c):
@Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
文檔僅供參考:http://developer.android.com/reference/android/app/AlarmManager.html – 2010-06-21 15:52:10