2010-06-21 45 views
4

我一直在搜索這個網站,並找到一些與設置鬧鐘有關的答案。我已經成功地設置了鬧鐘。如何設置即使重新啓動我的android手機也會關閉的鬧鐘?

我要做的就是:

  • 從我設置鬧鐘,在特定的時間和日期將調用接收器
  • 從接收我所說的服務
  • 從我送服務活動(在通知欄上)給用戶的通知。

我的問題是:

  1. 我建立了一個警報從現在起5分鐘。假設我關掉手機並重新打開(似乎忘記了鬧鐘)。我怎樣才能防止這種情況發生?

  2. 我真的需要調用一個服務來發送通知嗎,或者我可以從接收端進行嗎?

下面是關於前面的部分(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(); 
} 
+1

文檔僅供參考:http://developer.android.com/reference/android/app/AlarmManager.html – 2010-06-21 15:52:10

回答

2

你不需要調用服務來發送通知,你可以從接收器中完成。

我不認爲有一種方法可以在斷電後保存報警。 我該做什麼:

請注意,您需要將警報信息保存在某處。檢查Android的Data-storage

+0

哦哇! 非常感謝您的回答...我會嘗試讓你知道它是怎麼回事... 至於報警的信息保存我把它在表中(因爲會有一個報警每個項目)。但我想我會需要服務去看報警信息並檢查是否需要關閉。 乾杯:-) – monn3t 2010-06-21 17:24:10

+0

工作很好,正是我想要做的第一個地方。 Muchas Gracias,funcionóa laperfección。 – monn3t 2010-06-21 23:25:47

+0

喲!你能否發佈你的代碼的編輯版本? – ReachmeDroid 2011-12-14 05:46:50

相關問題