android setrepeatingalarm未被取消

f45qwnt8  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(204)

我花了很长时间试图弄明白为什么我的闹钟会重复 setRepeating() 不会被取消,但使用 setExact 被取消。
这就是我设置闹钟的方式。

Intent intent = new Intent(context.getApplicationContext(), NotificationAlarmReceiver.class);
        intent.setAction(Integer.toString(actionCode));
 PendingIntent pendingIntent =  PendingIntent.getBroadcast(context.getApplicationContext(), actionCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
            long time = System.currentTimeMillis() + delaySeconds* 1000L;
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, repeatIntervalSeconds, pendingIntent);

这就是我试图取消的方式:

protected void cancelScheduledReminderAlarms(Context context, int actionCode, String notificationType) {
        Logger.d("cancelScheduledAlarms PendingIntent ACTION = " + actionCode + " Notification type = "+ notificationType);
        Intent intent = new Intent(context.getApplicationContext(), NotificationAlarmReceiver.class);
        intent.setAction(Integer.toString(actionCode));
        PendingIntent pendingIntent =  PendingIntent.getBroadcast(context.getApplicationContext(), actionCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarms = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarms.cancel(pendingIntent);
        pendingIntent.cancel();
    }

我已经验证了两种方法中的actioncode都是700102。
感谢您的帮助。提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题