i'm start listening for location updates as soon as my application starts using (Network & GPS) providers, The time it takes for my location listener to receive the first location fix is often too long for users wait. Until a more accurate location is provided to my location listener, so i think i should utilize a cached location by calling getLastKnownLocation(String).
but really i'm little bit confusing where exactly i must put getLastKnownLocation(String) method in my code !!! before or after requestLocationUpdates()? in my function or in onResume() ??
please anyone help me, thanks in advance
this is my function:-
private final LocationListener gpsLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(networkLocationListener);
Toast.makeText(activity,"New GPS location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Toast.makeText(activity,"GPS available again\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(activity,"GPS out of service\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(activity,"GPS temporarily unavailable\n", Toast.LENGTH_SHORT).show();
break;
}}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(activity,"GPS Provider Enabled\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(activity,"GPS Provider Disabled\n",Toast.LENGTH_SHORT).show();
}
};
private final LocationListener networkLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(activity,"New network location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Toast.makeText(activity,"Network location available again\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(activity,"Network location out of service\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(activity,"Network location temporarily unavailable\n",Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(activity,"Network Provider Enabled\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(activity,"Network Provider Disabled\n",Toast.LENGTH_SHORT).show();
}
};
and this is code in onResume() function :-
locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, gpsLocationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 0,
networkLocationListener);