为了账号安全,请及时绑定邮箱和手机立即绑定

使用cancel()从AlarmManager删除警报-Android

使用cancel()从AlarmManager删除警报-Android

繁华开满天机 2019-12-27 14:44:36
我试图用两种不同的方法创建和删除警报,这两种方法在应用程序逻辑中的不同时刻都被调用。但是,当我调用AlarmManager的cancel()方法时,不会删除警报。这是我的addAlarm()方法: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]几个注意事项:RTC_WAKEUP #47 是我的原始警报RTC_WAKEUP #48 是应该覆盖#47而不是创建新警报的新警报。我使用filterEquals()返回的true...的Intent 方法比较了我的add和delete方法中的两个intent(不是前瞻性Intents),但是没有删除警报。我究竟做错了什么?
查看完整描述

3 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

试试这个标志:


PendingIntent.FLAG_UPDATE_CURRENT

代替:


PendingIntent.FLAG_CANCEL_CURRENT 

因此,PendingIntent将如下所示:


PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 

        alert.idAlert, intent, PendingIntent.FLAG_UPDATE_CURRENT)  

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


旁注:如果要一个全局AlarmManager,请将AlarmManager放在静态变量中(并且仅在时将其初始化null)。


查看完整回答
反对 回复 2019-12-27
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

在设置的任何特定时间从警报管理器中取消警报:


Intent intent = new Intent(context, AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);

am.cancel(pendingIntent);


查看完整回答
反对 回复 2019-12-27
  • 3 回答
  • 0 关注
  • 1052 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信