我創建了一些ProximityAlerts。這似乎只是第一件作品,第二件作品。他們似乎也過期了。我把這種方法來創建ProximityAlert:addProximityAlert最初的作品
public boolean addProximityAlert(int id) {
locationManager.addProximityAlert(
latitude,
longitude,
POINT_RADIUS,
PROX_ALERT_EXPIRATION,
getPendingIntent(id)
);
registerReceiver(new ProximityIntentReceiver(), getIntentFilter(id));
return true;
}
private PendingIntent getPendingIntent(int id) {
Intent intent = new Intent(PROX_ALERT_INTENT + id);
intent.setAction(String.valueOf(id));
return PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
private IntentFilter getIntentFilter(int id) {
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + id);
filter.addAction(String.valueOf(id));
return filter;
}
在ProximityIntentReceiver擴展廣播接收器:
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
message = context.getString(R.string.notification_alert_entering);
} else {
message = context.getString(R.string.notification_alert_exiting);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(context, context.getString(R.string.notification_alert), message, pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
}
變量:
private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000; // in Milliseconds
private static final long POINT_RADIUS = 500; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1;
private static final String PROX_ALERT_INTENT = "com.xxx.yyy.ProximityAlert";
我想創造多項獨有的警報,然後每次進入或退出鄰近時都會收到通知(可能會發生多次)。所有的警報應該保持有效,有人可以請我指出爲什麼這個工作不正常嗎?
你怎麼測試?您是否檢查是否將進入/退出POINT_RADIUS進入實際經/多頭位置?警報只會在穿越POINT_RADIUS時出現。 – Shrikant
@Shrikant更新後的問題。我使用真實設備進行測試,並從該位置移入和移出。 IE瀏覽器。創建一個警報,駕駛我的車2公里,然後開車回位置。 – powder366
你是否也有很好的互聯網連接?因爲你的代碼對我來說很好看。我也做了同樣的方式,它運作得很好(我幾乎準確的位置得到警報)。嘗試減少MINIMUM_TIME_BETWEEN_UPDATE(用於測試將其設置爲0) – Shrikant