2011-12-19 57 views
0

我想爲我的車庫門應用使用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); 
} 
+0

我遇到了同樣的問題。 在過去它對我來說很合適,因爲如果註冊近距離時,即時離開近距離,現在立即用isEntering = false發射,它根本不會發射。 ----我只需要知道它的設備特定或從某個Android版本。 – Yaadm 2015-11-11 17:27:29

回答

2

爲什麼不把定時器/延遲按鈕的發射?例如,爲什麼不在你的應用程序開始檢測之前讓自己有一段固定的時間才能離開鄰近區域?我認爲很多家庭報警器都以相同的方式工作。或者,另一種選擇可能是檢測並忽略您的第一次進入鄰近區域,並驗證任何後續重新進入該鄰近區域(即您的車庫)。你怎麼看?

+0

這是一個很好的建議。當我啓動近距離警報時,我也耽誤了時間...... – Ndupza 2014-05-30 08:24:24

相關問題