I am using Calendar class to obtain the current date and time. Then I set the date and time for the alarm and the information is passed to AlarmManager object. I have a button “Set Alarm” clicking on it should set the alarm, Somehow clicking the button gives no feedback.
Button btnOpen = (Button) findViewById(R.id.btnSetAlarm);
btnOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
timePicker = (TimePicker) findViewById(R.id.timePicker);
datePicker = (DatePicker) findViewById(R.id.datePicker);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0,
new Intent("my.todo.list.ShowNotification"), 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);
}
});
}
I am using Android version 4.0.1
Thanks.
First, clicking the button is not supposed to give you any feedback. You did not include any code to give you any feedback. All you did was set an alarm for some particular time.
Second, don’t use
getBaseContext()unless you have some specific reason to do so, and you do not have such a reason here. Usethis.Third, unless you have an activity with an
<intent-filter>that has an<action>ofmy.todo.list.ShowNotification, nothing will happen when the alarm time comes, other than a warning or error being dumped into LogCat.