【发布时间】:2015-09-24 16:17:05
【问题描述】:
用户有一个开关来激活或停用接近警报。
当用户激活此开关时,接近警报将以通知的方式显示。使用操作栏上的此通知,如果用户将开关更改为“停用”模式,则通知会消失。它工作正常。
但我的问题是这样的:
我激活了警报,但在那一刻它没有显示任何内容,因为我不在 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();
}
我读过这篇文章:
【问题讨论】:
标签: android notifications alert android-pendingintent proximity