Android 无法删除接近警报

Android 无法删除接近警报,android,notifications,alert,android-pendingintent,proximity,Android,Notifications,Alert,Android Pendingintent,Proximity,用户有一个开关来激活或停用接近警报 当用户激活此开关时,接近警报将以通知方式显示。通过操作栏上的此通知,如果用户将开关更改为“停用”模式,通知将消失。它工作正常 但我的问题是: 我激活了警报,但那一刻它没有显示任何内容,因为我没有接近POI。我改变了主意,现在,我不想要任何警报,所以我将开关更改为“停用”模式(操作栏上出现了任何通知) 但是,当开关打开“停用”模式时,当我接近POI时,警报将出现在操作栏上,我不知道为什么。 我正在使用removeProximityAlert和每个POI的挂起意图

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

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

但我的问题是:

我激活了警报,但那一刻它没有显示任何内容,因为我没有接近POI。我改变了主意,现在,我不想要任何警报,所以我将开关更改为“停用”模式(操作栏上出现了任何通知)

但是,当开关打开“停用”模式时,当我接近POI时,警报将出现在操作栏上,我不知道为什么。 我正在使用removeProximityAlert和每个POI的挂起意图的值

激活警报:

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);
}
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();
}
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);
要停用警报:

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);
}
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();
}
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);
我读过以下帖子:


这真的是一样的吗

激活:

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);
}
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();
}
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);
停用:

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);
}
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();
}
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);

请检查此方法,并确保您的意图与此处描述的标准一致。

您的问题在于使用
pendingent.FLAG_ONE_SHOT
。这并没有很好的文档记录,但如果删除该标志,它应该可以工作

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


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

是的,它是相同的值…我将该值存储在本地数据库(激活),然后加载每个POI(停用光标)的值。我已使用日志检查了它,是的,它具有相同的值。我已使用该方法测试代码,它返回“true”出于意图…它们是相同的=(让我知道这是否解决了您的问题,我将添加更多的解释。我现在无法这样做。我正在测试,而不是PendingEvent.FLAG_一次,值为0。从工作办公室看,它似乎可以正常工作(一会儿我将在实际情况下测试它),但是,你能解释一下为什么FLAG_ONE_SHOT不能正确工作吗?我不知道=(在我的回答中添加了一个解释。这是一个Android bug IMHO。请参见问题的不同之处,
FLAG_ONE_SHOT
的行为也会导致这种情况下的混乱。