2011-12-19 55 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

相关问题