-1

I'm creating the alarm with this code when my widget is added to screen:

    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, WidgetAlarmReceiver.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 60000, 60000, alarmIntent);

And I'm deleting the alarm using this code when no more widgets are present on the screen. I'm calling this code on the onReceive method of the AlarmReceiver class. Maybe that is the problem?

    AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent mIntent = new Intent(context, WidgetAlarmReceiver.class);
    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, mIntent, 0);
    alarmMgr.cancel(alarmIntent);

The problem is that the alarm is still getting called each minute even doing the cancel call.

What is wrong? The startAlarm code is not getting called anymore so I don't understand why the cancel doesn't works and the alarm gets called again and again.

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

0

Finally I solved it, by canceling the alarm from outside the alarm onReceive method itself. Maybe an alarm cannot cancel itself or something.

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382