[英]Impossible to remove proximity alert
用户可以通过开关来激活或停用接近警报。
当用户激活此开关时,接近警报将以通知方式显示。 通过操作栏上的此通知,如果用户将开关更改为“停用”模式,该通知将消失。 可以。
但是我的问题是这样的:
我激活了警报,但是由于我离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.FLAG_ONE_SHOT
。 没有充分的文档说明,但是如果删除该标志,它应该可以工作。
当您使用FLAG_ONE_SHOT
创建PendingIntent
时,Android不会跟踪此PendingIntent
(因为它只能使用一次)。 我认为这是一个“优化”,实际上是一个Android错误:-(
当您调用removeProximityAlert()
并将其传递给PendingIntent
,该方法中的代码将尝试取消所有具有匹配的 PendingIntent
接近警报。 由于Android不会跟踪使用FLAG_ONE_SHOT
创建的PendingIntent
,因此“匹配”代码永远不会找到您的接近警报,因此不会取消它。
这真的一样吗?
启用:
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), lastDigitsOfID, intent, PendingIntent.FLAG_ONE_SHOT);
禁用:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),cursor.getInt(idPendingIntentIndex), intent, 0);
请检查intentEquals()方法,并确保您的意图与此处描述的条件相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.