我遇到了同样的问题. 使用cancel()从Android的AlarmManager删除警报-Android
I have met same issue like this . Delete alarm from AlarmManager using cancel() - Android
" 我试图用两种不同的方法来创建和删除警报,这两种方法在应用程序的不同时刻都被调用.逻辑.
" I'm trying to create and delete an alarm in two different methods which are both called at different moments in the application. logic.
但是,当我调用AlarmManager的cancel()方法时,该警报不会被删除."
However when I call AlarmManager's cancel() method, the alarm isn't deleted."
要设置:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);为了删除:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.cancel(pendingIntent);但这不会删除已注册的警报. 提前致谢.
But this doesn't remove an alarm registered. Thanks in advance.
推荐答案PendingIntent必须与启动AlarmManager时完全一样地创建,并且看起来主要问题是您使用的是不同的requestCode(零而不是一).
The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're using a different requestCode (zero instead of one).
要快速修复,此方法应该有效:
For a quick fix, this should work:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, 0); alarmManager.cancel(pendingIntent);要使用PendingIntent.FLAG_UPDATE_CURRENT标志,请参见下文:
In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below:
设置:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);取消:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.cancel(pendingIntent);更多推荐
如何从AlarmManager取消警报
发布评论