[英]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.