使用取消()从AlarmManager删除报警 - 机器人 [英] Delete alarm from AlarmManager using cancel() - Android

查看:887
本文介绍了使用取消()从AlarmManager删除报警 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建并在两个不同的方法,它们都被称为在不同的时刻,在应用程序中删除报警。逻辑。

然而,当我打电话AlarmManager的取消()方法,报警不会被删除。

这是我的addAlarm()方法:

 意向意图=新的意图(PROX_ALERT_INTENT);
intent.putExtra(ALERT_TIME,alert.date);
intent.putExtra(ID_ALERT,alert.idAlert);
intent.putExtra(标题,alert.title);
intent.putExtra(GEO_LOC,alert.isGeoLoc);

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,alert.idAlert,意向,PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager =(AlarmManager)mContext
        .getSystemService(Context.ALARM_SERVICE);

台历挂历= Calendar.getInstance();
calendar.setTime(alert.date);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
        pendingIntent);
Log.e(添加报警 -  WithoutGeoLoc  - ,alert.toString());
 

这是我的deleteAlarm()方法:

  AlarmManager alarmManager =(AlarmManager)mContext
        .getSystemService(Context.ALARM_SERVICE);

意向意图=新的意图(PROX_ALERT_INTENT);
intent.putExtra(ALERT_TIME,alert.date);
intent.putExtra(ID_ALERT,alert.idAlert);
intent.putExtra(标题,alert.title);
intent.putExtra(GEO_LOC,alert.isGeoLoc);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
        alert.idAlert,意向,PendingIntent.FLAG_CANCEL_CURRENT);

alarmManager.cancel(pendingIntent);
Log.e(TAG,删除警报 - 无GeoLoc+ alert.toString());
 

这是我的logcat:

  01-23 17:44:07.411:E /添加报警 -  WithoutGeoLoc  - (18789):警报[纬度= 0.0,东经= 0.0,标题= bfwu,评论= NULL,地址=零,货币= HRK,idAlert = 1,日期=周六2月23日17时44分04秒CET 2013年,isGeoLoc = NULL]
01-23 17:44:13.032:E /删除警报没有GeoLoc  - (18789):警报[纬度= 0.0,东经= 0.0,标题= bfwu,评论= NULL,地址= NULL,货币= HRK,idAlert = 1,日期=周六2月23日17点44分04秒CET 2013年,isGeoLoc = NULL]
 

下面是pendingIntents在AlarmManager列表

 当前告警管理状态:
实时唤醒(现= 2013年1月23日17时44分37秒):

RTC_WAKEUP#48:报警{2c1d0588类型0 com.my.app}

类型= 0时= + 364d23h59m33s288ms repeatInterval重复= 0计数= 0

操作= PendingIntent {2c0a6cb0:PendingIntentRecord {2c1d04e8 com.my.app broadcastIntent}}

RTC_WAKEUP#47:报警{2c2298a0类型0 com.my.app}

类型= 0时= + 30d23h59m27s360ms repeatInterval重复= 0计数= 0

操作= PendingIntent {2c292af8:PendingIntentRecord {2c22a628 com.my.app broadcastIntent}}
 

RTC_WAKEUP#47 =>我原来报警 RTC_WAKEUP#48 =>新的警报应该有覆盖#47,而不是创建一个新的。

我比较了两个目的(不pendingIntents)从我的添加和删除方法的使用意图的 filterEquals()方法,该方法返回true 然而,报警不会被删除。 我究竟做错了什么?

*的更新

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

 私人无效saveAlert(){
// ***如果修改警报=>删除旧ALERT(再加入新)

意图int​​ent1 = NULL,intent2 = NULL;

    如果(alert.idAlert!= NULL)
    {
        如果(alert.isGeoLoc == NULL || alert.isGeoLoc == FALSE){
            intent2 = ProximityService.removeProximityAlertWithoutGeoLoc(getApplicationContext(),devisesApp.alertsGlobal.getAlertById(警报));
    }
    其他
    {
        ProximityService.removeProximityAlert(getApplicationContext(),警告);
    }
}
// ***添加警报
如果(alert.isGeoLoc == NULL || alert.isGeoLoc == FALSE){
    intent1 = ProximityService.addProximityAlertWithoutGeoLoc(getApplicationContext(),警戒,devisesApp.alertsGlobal);
} 其他
{
    ProximityService.addProximityAlert(getApplicationContext(),警戒,devisesApp.alertsGlobal);
}

Log.i(TAG,[saveAlert]警报ID:+ alert.idAlert);
devisesApp.alertsGlobal.addById(警报);
Log.i(故意等于,intent1.filterEquals(intent2)+); //这个返回true
}
 

解决方案

试试这个标志:

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

相反的:

  PendingIntent.FLAG_CANCEL_CURRENT
 

请确保您使用相同的警报对象,mContext。

如果你想要一个全球AlarmManager,把AlarmManager在一个静态变量(和初始化它只有为空)。

I'm trying to create and delete an alarm in two different methods which are both called at different moments in the application. logic.

However when I call AlarmManager's cancel() method, the alarm isn't deleted.

Here's my addAlarm() Method :

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());

Here's my deleteAlarm() 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);

alarmManager.cancel(pendingIntent);
Log.e(TAG,"REMOVE ALERT - without GeoLoc"+alert.toString());

Here's my 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]

Here's a list of pendingIntents in the AlarmManager

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 => My Original Alarm RTC_WAKEUP #48 => New Alarm that should have overwritten #47 rather than create a new one.

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

*UPDATE

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
}

解决方案

Try this flag:

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

Instead of:

PendingIntent.FLAG_CANCEL_CURRENT 

Make sure that you use same alert object and mContext.

If you want one global AlarmManager, put the AlarmManager in a static variable (and initialize it only if is null).

这篇关于使用取消()从AlarmManager删除报警 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆