0

I have tried to make an Android reminder but something has gone wrong; when I set a repeating notification in my app the notification keeps firing untill I uninstall the app (deleting the repeating notification does not prevent it from going off). Considering this, my reminders are deleted from the database when I delete them.

This issue is also the case with my repeating alarms.

What should I do?

here are some parts of my code:

// the delete query in database class

public boolean deleteReminder(long rowId) {

    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

// how I delete a reminder in my main activity

 @Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.menu_delete:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        mDbHelper.deleteReminder(info.id);
        fillData(); // populate
        return true;
    }
    return super.onContextItemSelected(item);
}

ReminderManager.java

public class ReminderManager {

private Context mContext; 
private AlarmManager mAlarmManager;

public ReminderManager(Context context) {
    mContext = context; 
    mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}

public void setReminder(Long taskId, Calendar when) {

    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId); 

    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); 

    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), 60 * 1000, pi);
}
}

ReminderService.java

public class ReminderService extends WakeReminderIntentService {

public ReminderService() {
    super("ReminderService");
        }

@Override
void doReminderWork(Intent intent) {
    Log.d("ReminderService", "Doing work.");
    Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
    notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
    note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
    note.defaults |= Notification.DEFAULT_SOUND; 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 

    int id = (int)((long)rowId);
    mgr.notify(id, note); 


}
}

OnAlarmReceiver.java

public class OnAlarmReceiver extends BroadcastReceiver {

private static final String TAG = ComponentInfo.class.getCanonicalName(); 


@Override   
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Received wake up from alarm manager.");

    long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

    WakeReminderIntentService.acquireStaticLock(context);

    Intent i = new Intent(context, ReminderService.class); 
    i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid);  
    context.startService(i);

}
}

EDIT

the issue in my case is not cancelling alarms, I'm wondering why when I even delete reminders in my app they just keep going off.

pablo
  • 31
  • 5
  • Possible duplicate of [How to cancel this repeating alarm?](http://stackoverflow.com/questions/3330522/how-to-cancel-this-repeating-alarm) – Mike M. Jun 12 '16 at 05:12
  • Then where are you canceling the alarms? They aren't going to stop just because you delete an entry in your app's database. – Mike M. Jun 12 '16 at 05:28
  • @Mike M, Thank you so much. You are right, I had to cancel my alarms as well. – pablo Jun 12 '16 at 05:56

2 Answers2

0

You will have to call AlarmManager.cancel(PendingIntent) with the exact same PendingIntent when you call setRepeating in order to cancel the alarm.

CarlLee
  • 3,952
  • 5
  • 23
  • 33
0

You should cancel the pendingIntent that which you created for repeating alarm. Write code like buttonclick that will execute the

AlarmManager.cancel(pendingIntent)

Make sure you are providing the same pending event that you used in setting . Or else the other pendingIntent which matches with the supplied will get cancelled.