我想爲我的車庫門應用使用Android的Proximity Alert。我有一個按鈕設置,當按下添加的接近警報並開始跟蹤位置。我希望它無限期地跟蹤,直到我進入或退出接近度。如果進入它將打開車庫門,如果退出它將關閉車庫門,之後我刪除接近警報和GPS關閉。我的工作接近完美。在接近設置的情況下啓動Android GPS Proximity Alert
問題是,當我在車道上按下按鈕時,因此在附近,意圖總是立即觸發,額外的KEY_PROXIMITY_ENTERING設置爲「進入」。如果我在鄰近範圍之外開始跟蹤,它會按預期行動,只有在我進入鄰近區域後纔會觸發。如何從內部開始以相同方式工作時退出鄰近區域?在此先感謝,這是我的代碼。
在我的活動:
float radius = 100f;
double lat = 37.422;
double lon = -122.084;
// Expiration is 10 Minutes
long expiration = 600000;
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Intent intent = new Intent(proximityIntentAction);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
locManager.addProximityAlert(lat, lon, radius, expiration, pendingIntent);
這裏是我的廣播接收器:
public void onReceive(Context context, Intent intent)
{
Log.v("SomeTag","Proximity Alert Intent Received");
String direction = LocationManager.KEY_PROXIMITY_ENTERING;
CharSequence text = direction;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "going: "+text, duration);
toast.show();
LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_NO_CREATE);
locManager.removeProximityAlert(pendingIntent);
}
我遇到了同樣的問題。 在過去它對我來說很合適,因爲如果註冊近距離時,即時離開近距離,現在立即用isEntering = false發射,它根本不會發射。 ----我只需要知道它的設備特定或從某個Android版本。 – Yaadm 2015-11-11 17:27:29