我正在制作一个简单的位置提醒,用户在地图上选择一个地址,然后用它创建一个proximityAlert。
我的第一个想法是,如果甚至可以在该时间范围内到达目的地,则仅搜索新位置。 F.E.如果目标距离是80英里,则不断查询该位置是没有意义的,因为距离车辆大约需要一个小时。 我发现了另一种方法here 它只会在驾驶时获得位置更新。
我几乎陷入了onHandleIntent()方法:我不知道该怎么做。我很高兴任何投入
public class MyMapActivity extends Activity implements OnInfoWindowClickListener,
OnMarkerClickListener, OnMapLongClickListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener
{
private LocationClient mLocationClient;
private PendingIntent mPendingIntent = null;
private LocationRequest mLocationRequest = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gmaps);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setInfoWindowAdapter(new MyInfoWindowAdapter(this, getLayoutInflater()));
map.setOnMapLongClickListener(this);
map.setOnInfoWindowClickListener(this);
map.setOnMarkerClickListener(this);
String locationProvider = findBestLocationProvider();
if(locationProvider != null) {
mMyLocation = mLocationManager.getLastKnownLocation(locationProvider);
}
Intent intent = new Intent( getApplicationContext(), LocationIntentService.class);
intent.putExtra(LOCATION_KEY, mMyLocation);
mPendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
public void onConnected(Bundle arg0) {
Log.d(TAG, "onConnected()");
if(mLocationClient != null) {
mLocationClient.requestLocationUpdates(mLocationRequest, mPendingIntent);
}
}
@Override
public void onDisconnected() {
Log.d(TAG, "onDisconnected()");
}
}
服务:
public class LocationIntentService extends IntentService {
private Location mLocation = null;
@Override
protected void onHandleIntent(Intent intent) {
Log.d(tag, "caught an intent");
if(intent.hasExtra(MyMapActivity.LOCATION_KEY)) {
Location loc = (Location) intent.getExtras().get(MyMapActivity.LOCATION_KEY);
Log.d(tag, "onHandleIntent(): new location: " + loc.toString());
// do calculation here.
// how would I get access to the location client to removeUpdates and/or requestLocationUpdates
// remove locationUpdates and start Alarm to start locationUpdates in X minutes
mLocation = loc;
}
}