使用 cancel() 从 AlarmManager 删除警报

编程入门 行业动态 更新时间:2024-10-25 16:18:17
本文介绍了使用 cancel() 从 AlarmManager 删除警报 - 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.

这是我的 addAlarm() 方法:

Here's my addAlarm() Method :

AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(PROX_ALERT_INTENT); intent.putExtra("ALERT_TIME", alert.date); intent.putExtra("ID_ALERT", alert.idAlert); intent.putExtra("TITLE", alert.title); intent.putExtra("GEO_LOC", alert.isGeoLoc); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alert.idAlert, intent, PendingIntent.FLAG_CANCEL_CURRENT); Calendar calendar = Calendar.getInstance(); calendar.setTime(alert.date); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); Log.e("ADD ALERT - WithoutGeoLoc - ",alert.toString());

这是我的 deleteAlarm() 方法:

AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(PROX_ALERT_INTENT); intent.putExtra("ALERT_TIME", alert.date); intent.putExtra("ID_ALERT", alert.idAlert); intent.putExtra("TITLE", alert.title); intent.putExtra("GEO_LOC", alert.isGeoLoc); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alert.idAlert, intent, PendingIntent.FLAG_CANCEL_CURRENT); alarmManager.cancel(pendingIntent); Log.e(TAG,"REMOVE ALERT - without GeoLoc"+alert.toString());

这是我的 Logcat:

01-23 17:44:07.411: E/ADD ALERT - WithoutGeoLoc -(18789): Alert [latitude=0.0, longitude=0.0, title=bfwu, comments=null, address=null, currency=hrk, idAlert=1, date=Sat Feb 23 17:44:04 CET 2013, isGeoLoc=null] 01-23 17:44:13.032: E/REMOVE ALERT without GeoLoc - (18789): Alert [latitude=0.0, longitude=0.0, title=bfwu, comments=null, address=null, currency=hrk, idAlert=1, date=Sat Feb 23 17:44:04 CET 2013, isGeoLoc=null]

这是AlarmManager中的pendingIntent列表

Current Alarm Manager state: Realtime wakeup (now=2013-01-23 17:44:37): RTC_WAKEUP #48: Alarm{2c1d0588 type 0 com.my.app} type=0 when=+364d23h59m33s288ms repeatInterval=0 count=0 operation=PendingIntent{2c0a6cb0: PendingIntentRecord{2c1d04e8 com.my.app broadcastIntent}} RTC_WAKEUP #47: Alarm{2c2298a0 type 0 com.my.app} type=0 when=+30d23h59m27s360ms repeatInterval=0 count=0 operation=PendingIntent{2c292af8: PendingIntentRecord{2c22a628 com.my.app broadcastIntent}}

一些注意事项:

  • RTC_WAKEUP #47 是我原来的闹钟
  • RTC_WAKEUP #48 是一个新闹钟,应该覆盖 #47 而不是创建一个新闹钟.
  • RTC_WAKEUP #47 is My Original Alarm
  • RTC_WAKEUP #48 is a New Alarm that should have overwritten #47 rather than create a new one.

我使用返回 true 的 Intent 的 filterEquals() 方法比较了我的添加和删除方法中的两个意图(不是pendingIntents)......但警报不是删除.我做错了什么?

I have compared the two intents (not pendingIntents) from my add and delete methods using Intent's filterEquals() method which returns true ... Yet the alarm is not deleted. What am I doing wrong?

更新

这是我的 saveAlert() 方法,它调用 addAlarm() 和 deleteAlarm()

Here's my saveAlert() method that calls addAlarm() and deleteAlarm()

private void saveAlert() { // *** If Modifying Alert => REMOVE OLD ALERT (then add new one) Intent intent1 = null, intent2 = null; if (alert.idAlert != null) { if (alert.isGeoLoc == null || alert.isGeoLoc == false) { intent2 = ProximityService.removeProximityAlertWithoutGeoLoc(getApplicationContext(), devisesApp.alertsGlobal.getAlertById(alert)); } else { ProximityService.removeProximityAlert(getApplicationContext(), alert); } } // *** Add Alert if (alert.isGeoLoc == null || alert.isGeoLoc == false) { intent1 = ProximityService.addProximityAlertWithoutGeoLoc(getApplicationContext(), alert, devisesApp.alertsGlobal); } else { ProximityService.addProximityAlert(getApplicationContext(), alert, devisesApp.alertsGlobal); } Log.i(TAG, "[saveAlert] Alert ID : " + alert.idAlert); devisesApp.alertsGlobal.addById(alert); Log.i("INTENT EQUALS", intent1.filterEquals(intent2) + ""); // This returns true }

推荐答案

试试这个标志:

PendingIntent.FLAG_UPDATE_CURRENT

代替:

PendingIntent.FLAG_CANCEL_CURRENT

因此 PendingIntent 将如下所示:

So the PendingIntent will look like this:

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alert.idAlert, intent, PendingIntent.FLAG_UPDATE_CURRENT)

(确保您使用相同的alert 对象和mContext!)

(Make sure that you use same alert object and mContext!)

旁注:如果你想要一个全局的 AlarmManager,把 AlarmManager 放在一个静态变量中(并且只有当它是 null 时才初始化它).

A side note: If you want one global AlarmManager, put the AlarmManager in a static variable (and initialize it only if it's null).

更多推荐

使用 cancel() 从 AlarmManager 删除警报

本文发布于:2023-11-27 01:39:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   cancel   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!