使用cancel() – Android从AlarmManager中删除闹钟
我试图用两种不同的方法创build和删除报警,这两种方法在应用程序中的不同时刻都被调用。 逻辑。
但是,当我调用AlarmManager的cancel()
方法时,警报不会被删除。
这是我的addAlarm()方法:
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 alarmManager = (AlarmManager) mContext .getSystemService(Context.ALARM_SERVICE); 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中的pendingIntents列表
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,而不是创build一个新的。
我使用Intent的filterEquals()
方法从添加和删除方法中比较了两个intents(Not filterEquals()
,它返回true,但是报警不会被删除。 我究竟做错了什么?
* 更新
这是我的saveAlert()方法,它调用addAlarm()和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 pendingIntent = PendingIntent.getBroadcast(mContext, alert.idAlert, intent, PendingIntent.FLAG_UPDATE_CURRENT)
代替:
PendingIntent.FLAG_CANCEL_CURRENT
确保您使用相同的警报对象和mContext。
如果你想要一个全局的AlarmManager,把AlarmManager放在一个静态variables中(并且只有当它是null时才初始化它)。
取消闹钟有点混乱。 你必须传递相同的ID和IntentPending。 这里是一个例子:
private void resetIntentWithAlarm(int time){ Intent intentAlarm = new Intent(getApplicationContext(), DownloadService.class); intentAlarm.putExtra(Your Key, Your stuff to pass here); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getService( getApplicationContext(), YOUR_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT ); if (time != 0) { alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (60L * 1000L * time), (60L * 1000L * time), pendingIntent); Log.i(TAG, "Alarm setted on for " + time + " mins."); } // if TIME == Zero, cancel alaram else { alarmManager.cancel(pendingIntent); Log.i(TAG, "Alarm CANCELED. Time = " + time); }
如果你想取消警报pipe理器,只需把赖特编号置于挂起的意图中,只需在报警pipe理器中为该挂起的意图调用cancel()即可。