我遇到过这样的问题.
Delete alarm from AlarmManager using cancel() – Android
“
我正在尝试使用两种不同的方法创建和删除警报,这两种方法都在应用程序的不同时刻调用.逻辑.
但是,当我调用AlarmManager的cancel()方法时,警报不会被删除.“
为了设置:
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);
但是这并没有删除已注册的警报.
提前致谢.
解决方法:
需要创建PendingIntent,就像启动AlarmManager时一样,看起来主要的问题是你使用的是另一个requestCode(零而不是一个).
为了快速解决,这应该工作:
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标志,请参阅以下内容:
设置:
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);
总结 以上是内存溢出为你收集整理的android – 如何取消AlarmManager的报警全部内容,希望文章能够帮你解决android – 如何取消AlarmManager的报警所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)