そのため、AlarmManager を使用してアラームを設定するためのチュートリアルに従ってきましたが、何が間違っていたのかを理解しようとしています。onReceive メソッドをオーバーライドする必要がありますか?
私は 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 を使用し、アラームが発生したことを表示するためのものです。
@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();
}