1

我想为我的车库门应用程序使用 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);
}
4

1 回答 1

2

为什么不在按钮的触发上设置一个计时器/延迟?例如,为什么不在您的应用开始检测之前让自己有一段固定的时间离开附近?我认为许多家庭警报器的工作方式相同。或者,另一种选择可能是检测并忽略您第一次进入邻近区域并验证任何后续重新进入该邻近区域(即您的车库)。你怎么看?

于 2013-03-19T11:51:15.417 回答