Reputation: 353
I am new to Android, and i am creating Date Reminder app where user will enter date, time and message to be notified on the entered date & time. Till now i have achieved Creating Alarm, Showing Notification, On Click of notification, opening the App with details of that notification and update if required, Listing the All the Alarm (I used SqlLite to store data).
On my Alarm Listing Page, On click of item, i am showing AlertDialog box with option to delete, when i click on delete i am deleting the Alarm details from database, but unable to cancel the alarm.
Here is the code from 2 classes, one where i am creating the alarm, and other where i am cancelling the alarm, not sure what i am missing!!!!
Class CreateActivity extends Activity
methode setAlarm{
.....
Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
db = new DatabaseHandler(this);
if(!isNotified) //create new alarm
{
Random random = new Random();
alarmId = random.nextInt(10000);
pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
db.addReminder(pojo);
System.out.println("Adding Reminder.");
}else{ // Its from notification
pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
db.updateReminder(pojo);
System.out.println("Updating Reminder.");
}
myIntent.putExtra("alarmId", alarmId);
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
....
}
Here AlarmId is unique passed to PendingIntent
Here is another List activity where on click on AlertDialog i am deleting the Alarm from database and trying to cancel the Alarm manager. Here i used same AlarmId which was used to create the PendingIntent
Class MyListActivity extends ListActivity
....
private void deleteReminder(int alarmId) {
System.out.println("alarmId= "+ alarmId);
DatabaseHandler db = new DatabaseHandler(this);
DateBookPojo pojo = new DateBookPojo();
pojo.setReminderId(alarmId);
db.deleteReminder(pojo); // delete alarm details from database
System.out.println("Deleted Entry. = > " + alarmId);
Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
System.out.println("Cancelled Alarm. = > " + alarmId);
showList(); // Again show the new list.
}
I googled & stackoverflow a lot and but everywhere i saw this solution working fine, but its not working for me.
One think i noted here, Onlick of notification, i am using the CreateActivity
class to show details and when updated, its updating the notified alarm. I guess i am messing something around the Context related to Intent
& PendingIntent
.
Please help me.
Upvotes: 0
Views: 2061
Reputation: 12526
Refer this. Delete alarm from AlarmManager using cancel() - Android
According to answer provided there your pendingIntent
in delete method should be declared as
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Upvotes: 0
Reputation: 14274
Add this:
myIntent.putExtra("alarmId", alarmId);
to your deleteReminder()
method (or remove it from setAlarm
).
In order to remove a PendingIntent
it has to match exactly.
Upvotes: 3