how to cancel scheduled task
hello i have to set one alarm for every appointment(which i insert appointment details) and also show list of appointments on which date it to be alarm. but if i cancel particular ap开发者_如何学Cpointment from list then it must not alarm but for all appointments it is being alarm .
how i cancel any specific alarm for appointment.
calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, _year);
calendar.set(Calendar.MONTH, _month - 1);
calendar.set(Calendar.DATE, _date);
calendar.set(Calendar.HOUR_OF_DAY, _hour1);
calendar.set(Calendar.MINUTE, _min1);
calendar.set(Calendar.SECOND, 0);
Date specifiedTime = calendar.getTime();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
showNotification();
}
}, specifiedTime);
i have already use timer.cancel(); but it won't work properly. Please help... Thanks in advance
Use indexing to each appointment to avoid this problem through a loop between the appointments.
you should use the AlarmService instead of a timer. The Link specified also includes an example how to use it.
You can use it to notify a broadcast receiver, which starts a service or an activity for example.
If you use the same intent as you have sent to activate an alarm to cancel it, it will work. It does not need to be the same instance of the object, the same intent text will be enough.
Hope this helps.
精彩评论