无法删除接近警报
Impossible to remove proximity alert
用户有一个开关来激活或停用接近警报。
当用户激活此开关时,接近警报将以通知的方式显示。对于操作栏上的此通知,如果用户将开关更改为 "deactivate" 模式,则通知会消失。没问题。
但我的问题是:
我激活了警报,但那一刻它没有显示任何内容,因为我不在 POI 附近。我改变了主意,现在我不想要任何警报,所以我将开关更改为 "deactivate" 模式(任何通知都出现在操作栏上)。
但是,切换到"deactivate"模式后,当我靠近一个兴趣点时,操作栏上会出现警报,我不知道为什么。
我将 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();
}
我读过这篇文章:
Post1
Post2
Post3
这真的一样吗?
激活:
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);
停用:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);
请查看 intentEquals() 方法并确保您的意图与此处描述的标准相同。
您的问题是 PendingIntent.FLAG_ONE_SHOT
的使用。这没有很好的记录,但如果您删除该标志,它应该可以工作。
当您使用 FLAG_ONE_SHOT
创建 PendingIntent
时,Android 不会跟踪此 PendingIntent
(因为它只允许使用一次)。在我看来,这是一个 "optimization",实际上是一个 Android 错误 :-(
当您调用 removeProximityAlert()
并将其传递给 PendingIntent
时,该方法中的代码会尝试取消所有具有 匹配项 PendingIntent
。由于 Android 不跟踪使用 FLAG_ONE_SHOT
创建的 PendingIntent
,"matching" 代码永远找不到您的接近警报,因此它不会取消它。
用户有一个开关来激活或停用接近警报。
当用户激活此开关时,接近警报将以通知的方式显示。对于操作栏上的此通知,如果用户将开关更改为 "deactivate" 模式,则通知会消失。没问题。
但我的问题是:
我激活了警报,但那一刻它没有显示任何内容,因为我不在 POI 附近。我改变了主意,现在我不想要任何警报,所以我将开关更改为 "deactivate" 模式(任何通知都出现在操作栏上)。
但是,切换到"deactivate"模式后,当我靠近一个兴趣点时,操作栏上会出现警报,我不知道为什么。 我将 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();
}
我读过这篇文章:
Post1
Post2
Post3
这真的一样吗?
激活:
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);
停用:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);
请查看 intentEquals() 方法并确保您的意图与此处描述的标准相同。
您的问题是 PendingIntent.FLAG_ONE_SHOT
的使用。这没有很好的记录,但如果您删除该标志,它应该可以工作。
当您使用 FLAG_ONE_SHOT
创建 PendingIntent
时,Android 不会跟踪此 PendingIntent
(因为它只允许使用一次)。在我看来,这是一个 "optimization",实际上是一个 Android 错误 :-(
当您调用 removeProximityAlert()
并将其传递给 PendingIntent
时,该方法中的代码会尝试取消所有具有 匹配项 PendingIntent
。由于 Android 不跟踪使用 FLAG_ONE_SHOT
创建的 PendingIntent
,"matching" 代码永远找不到您的接近警报,因此它不会取消它。