首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不可能解除邻近警报

不可能解除邻近警报
EN

Stack Overflow用户
提问于 2015-09-23 10:50:35
回答 2查看 246关注 0票数 1

用户有一个开关来激活或停用邻近警报。

当用户激活此开关时,邻近警报将以通知方式显示。在操作栏上使用此通知时,如果用户将切换更改为“停用”模式,则通知将消失。工作正常。

但我的问题是:

我激活警报,但它没有显示任何在那一刻,因为我没有接近一个POI。我已经改变了主意,现在,我不需要任何警告,所以我将切换到“停用”模式(任何通知都出现在操作栏上)。

但是,在切换到“停用”模式时,当我接近POI时,警报将出现在操作栏上,我不知道为什么。我使用的removeProximityAlert值为每个POI的待定意图。

激活警报:

代码语言:javascript
复制
public void activateAlerts() {

    Database db = new Database(getApplicationContext());
    Cursor cursor = db.getPois();

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


    while (cursor.moveToNext()) {

        ...
        Intent intent = new Intent(PROX_ALERT_INTENT);

        String idSubstring = id.substring(7);
        int lastDigitsOfID = Integer.parseInt(idSubstring);
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);

        try {
            locationManager.addProximityAlert(latitude, longitude, Utils.RADIOALERTS, -1, pi);
        } catch (Exception e) {
            Log.e(getClass().getSimpleName().toString(), e.toString());
        }
        notifications.add(new NotificationObject(cursor.getString(idIndex), lastDigitsOfID));
    }
    db.addNotifications(notifications);
}

禁用警报:

代码语言:javascript
复制
public void deactivateAlerts() {

    Database db = new Database(getApplicationContext());
    Cursor cursor = db.getIdPendingIntents();

    int idPendingIntentIndex = cursor.getColumnIndex("id_pendingintent");
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    while (cursor.moveToNext()) {
        Intent intent = new Intent(PROX_ALERT_INTENT); 
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                cursor.getInt(idPendingIntentIndex), intent, 0);
        locationManager.removeProximityAlert(pendingIntent);
    }

    NotificationManager notificationManager =
            (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll();
}

我读过这篇文章:

Post1

Post2

Post3

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-23 20:21:47

您的问题是PendingIntent.FLAG_ONE_SHOT的使用。这不是很好的文档,但是如果您移除那个标志,它应该会工作。

当您使用PendingIntent创建FLAG_ONE_SHOT时,Android不会跟踪这个PendingIntent (因为它只允许使用一次)。在我看来,这是一个“优化”,实际上是一个Android bug :-(

当您调用removeProximityAlert()并将其传递给PendingIntent时,该方法中的代码将尝试取消所有具有匹配的 PendingIntent的邻近警报。由于Android不跟踪用PendingIntent创建的FLAG_ONE_SHOT,所以“匹配”代码永远找不到您的接近警报,因此不会取消它。

票数 1
EN

Stack Overflow用户

发布于 2015-09-23 11:16:44

这真的一样吗?

激活:

代码语言:javascript
复制
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);

停用:

代码语言:javascript
复制
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);

请查看intentEquals()方法,并确保您的意图与这里描述的标准相同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32737603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档