【发布时间】:2011-12-19 01:39:57
【问题描述】:
我想为我的车库门应用使用 Android 的 Proximity Alert。我有一个按钮设置,按下时添加接近警报并开始跟踪位置。我希望它无限期地跟踪,直到我进入或退出邻近区域。如果进入它将打开车库门,如果退出它将关闭车库门,然后我移除接近警报并且 GPS 关闭。我让它工作得近乎完美。
问题是,当我在车道上按下按钮时,因此在附近时,意图总是立即触发,并将额外的 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 版本。
标签: android gps locationmanager proximity