简体   繁体   中英

Alarmmanager not cancel after clicking notification

I am developing an app with alarmmanager and services and I cannot cancel the notification in certain cases. I am using fragments.

My app send two notifications every "x" different minutes. I have a button to start the service and another one to stop it. When I start the service the notifications are fired and when I press the stop button it works correctly. But when I start the service and I exit the app, the notifications are fired normally but when I click a notification, the app is opened and then I press the stop button but the notifications never stop, the notifications continue being fired.

The function where I call alarmanager.cancel(pendingintent) is in the same fragment.. (MainFragment.class which is the first fragment).

MainFragment.class

    intent = new Intent(getContext(), TimeAlarm.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    pendingIntent = PendingIntent.getBroadcast(mContext, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 15000,
                15000,
                pendingIntent);

    intent2 = new Intent(getContext(), TimeAlarm2.class);
    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
    intent2.setAction(Long.toString(System.currentTimeMillis()));
    pendingIntent2 = PendingIntent.getBroadcast(getContext(), 1,
            intent2, PendingIntent.FLAG_UPDATE_CURRENT);

    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime() + 15000,
                        15000,
                        pendingIntent2);

TimeAlarm.class which extends BroadcastReceiver

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle("...")
                .setContentText("...");

 Intent resultIntent = new Intent(context, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        mBuilder.setAutoCancel(true);

        mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());

TimeAlarm2.class which extends BroadcastReceiver

NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.icon2)
                .setContentTitle("..")
                .setContentText("..");

        Intent resultIntent = new Intent(context, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder2.setContentIntent(resultPendingIntent);

        mBuilder2.setAutoCancel(true);

        mBuilder2.build().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder2.build());

Thanks and best regards!

Remove these lines from both intent and try it again.

intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent2.setAction(Long.toString(System.currentTimeMillis()));

It is working for me

Try this and if the error comes telling me what happens.

For that you need to destroy the service on the stop button and again start service on start button click.

Okay. I can suggest you to call cancel() on AlarmManager with an equivalent PendingIntent to the one you used with setRepeating() method.

Here you can cancel both of your equivalent pendingIntent.

am.cancel(pendingIntent);

and

am.cancel(pendingIntent2);

Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM