在我的应用程序中,我有一个包含关联经纬度位置的第 9 轮地点列表,最终目标是通知用户他们是否在任何位置的大约半英里范围内。
最好的方法是什么?这就是我到目前为止所拥有的。
distanceFromListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
List<JSONObject> centres = sortVenuesByNearest(location);
Log.d("FUApp", "distanceFromListener setting location");
haveLocation = true;
theLocation = location;
float accuracy = location.getAccuracy();
for (JSONObject centre : centres) {
try {
Double distance = Double.valueOf(centre.getString("distanceTo"));
String name = centre.getString("name");
Integer venueID = Integer.valueOf(centre.getString("id"));
if (distance < 804.672) {
Bundle mBundle = new Bundle();
Double miles = (distance * 0.00062137);
DecimalFormat df = new DecimalFormat("#.##");
String miles2 = df.format(miles);
mBundle.putString(ExtraKeyCentreName, name);
mBundle.putString(ExtraKeyCentreDist, miles2 + " miles away");
mBundle.putString(ExtraKeyCentreJSON, centre.toString());
mBundle.putString(ExtraKeyCentrePostcode, "none");
sendNotification(ActivityCentrePage.class, "Visit " + name, "You're half a mile from " + name + ". Why not come for a visit?", R.drawable.ic_launcher, venueID, mBundle);
} else {
cancelNotification(venueID);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
Boolean notify_if_close = sp.getBoolean(SettingsNotifyIfClose, true);
if (notify_if_close) {
locationService.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 100, distanceFromListener);
}
这行得通,但是如果用户取消了一个说那里关闭的通知,那么当下次更新位置时,它会再次弹出,这会很烦人。是否会在 requestLocationUpdates 上设置一个大的 minTime,比如半小时?