0
所以我一直在使用AlarmManager來設置鬧鐘的教程,我試圖弄清楚我做錯了什麼。我需要重寫onReceive方法嗎?使用AlarmManager設置鬧鐘不會觸發
我使用DatePicker和TimePicker小部件並從他們和用戶的輸入中獲取我的數據。我在這裏設置鬧鐘的主要類是:
public void initControls() {
timePicker = (TimePicker)findViewById(R.id.timePicker);
datePicker = (DatePicker)findViewById(R.id.datePicker);
setAlarm = (Button)findViewById(R.id.setAlarm);
myCal = Calendar.getInstance();
setAlarm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
myCal.set(Calendar.YEAR, datePicker.getYear());
myCal.set(Calendar.MONTH, datePicker.getMonth());
myCal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
myCal.set(Calendar.HOUR, timePicker.getCurrentHour());
myCal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
myCal.set(Calendar.SECOND, 0);
Intent triggered = new Intent("alarms.DisplayNotification");
triggered.putExtra("NotificationId", 1);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0, triggered, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
myCal.getTimeInMillis(), displayIntent);
}
});
}
我正在使用的另一個類是使用notificationBar並顯示Alarm已觸發。
@Override
public void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
int notifID = getIntent().getExtras().getInt("NotificationId");
Intent i = new Intent("com.example.mt_study.Main_screen");
i.putExtra("NotificationId", notifID);
PendingIntent displayAlarm = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.book,
"Time's up!",
System.currentTimeMillis());
CharSequence from = "AlarmManager - Thats all";
CharSequence message = "Alarm DONE";
notif.setLatestEventInfo(this, from, message, displayAlarm);
//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notifID, notif);
//---destroy the activity---
finish();
}
另一個選擇是讓第二個活動在清單中實現一個意圖過濾器,它攔截「alarms.DisplayNotification」 – fedepaol 2013-04-07 17:11:39
感謝您的快速響應!我在看教程時覺得這很奇怪,但是我改變了它,似乎沒有效果。我現在提前一分鐘設置鬧鐘。 – cj1098 2013-04-07 17:14:48
我已經縮小到displayNotifications類。我想我可以從這裏處理它。謝謝您的幫助 :) – cj1098 2013-04-07 17:21:09