by 0x1B » Tue Jan 13, 2009 1:57 am
Mine looks more like this--->
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria,true);
if (provider != null) {
location = locationManager.getLastKnownLocation(provider);
/* TODO - adjust update times to minimize battery use. Can be changed as needed.
*
*/
locationManager.requestLocationUpdates(provider,
60000, // 1min
100, // 100m
locationListener);
}
Obviously you don't need to request location updates, but if you do then below is some code I use....
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location newloc) {
// something to do here when location changes
// TODO get other datq (accuracy, velocity, altitude, etc...)
// write data to database
// add markers
location = newloc;
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
and you need to remember to turn off/on the updating at onStart and onStop
@Override
public void onStop() {
super.onStop();
if (provider != null) {
locationManager.removeUpdates(locationListener);
}
@Override
public void onStart() {
super.onStart();
/* recheck which provider to use */
provider = locationManager.getBestProvider(criteria,true);
if (provider != null) {
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider,
60000, // 1min
100, // 100m
locationListener);
}
}
Hope this helps.
________________
English: Fluent
Français: écrit, lu, parlé
Español: escrito, leído, hablado