Alarmmanager does not cancel scheduled alarm

Asked

Viewed 189 times

1

I have the following code on MyReceiver:

@Override
    public void onReceive(Context context, Intent intent){

        context.startService(new Intent(context, BackgroundService.class));
    }

    public void setAlarm(Context context, long mills){

        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, ReceiverNotification.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 2310, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        am.setRepeating(AlarmManager.RTC_WAKEUP, (mills + 60000), 120000, pi);
    }

    public void setAlarmCancel(Context context){

        Intent intent = new Intent(context, MyReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 2310, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        alarmManager.cancel(sender);

        Log.v(TAG, "Alarme cancelado!");

    }

And I got this for the notifications:

public class ReceiverNotification extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent){
        context.startService(new Intent(context, NotificationService.class));
    }
}

When I schedule for a certain time, call the method setAlarmCancel(), I have the return of LOG but the alarm continues. I would appreciate a help ...

  • 1

    From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

  • 1

    @rrnan, made ;)

1 answer

1

I changed the line Intent intent = new Intent(context, MyReceiver.class); for Intent intent = new Intent(context, ReceiverNotification.class);

public void setAlarmCancel(Context context){

        Intent intent = new Intent(context, ReceiverNotification.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 2310, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        alarmManager.cancel(sender);

        Log.v(TAG, "Alarme cancelado!");

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.