0

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);
2NE1
  • 13
  • 4
  • You should put it before requestLocationUpdates() as it will set the last know location and wait for more accurate location. Secondly, you should not call it on onResume() as it will make it to call this method again and again with the lifecycle of activity/fragment. – Umesh Chauhan Sep 24 '16 at 19:48

1 Answers1

0

lastKnownLocation() should be called before requestLocationUpdates() but I've noticed you call removeUpdates() once it has picked up a location. Perhaps you want to use requestSingleUpdate instead?

https://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String, android.app.PendingIntent)

It should be noted that in this answer they say you should wait for the GPS to stabilise and to avoid using requestSingleUpdate() but if you're not after huge accuracy it would be better.

Community
  • 1
  • 1
Josh Laird
  • 6,974
  • 7
  • 38
  • 69
  • no, i'm not really want to use `requestSingleUpdate()` , i'm calling removeUpdates() because I use first “network” provider first, and then fallback on “GPS” when it's available and remove network provider – 2NE1 Sep 25 '16 at 14:34