Search code examples
androidnullgpsdelay

Any way to delay getLatKnownLocation? Android GPS


So i am using the getLastKnownLocation method to get the Location loc of the device but it returns null unless there is a GPS fix. Is there a way to delay execution of getLastKnownLocation until the GPS has a valid location to provide?

package test.example;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class testGPS extends Activity 
{
    private LocationManager lm;
    private LocationListener locationListener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);


        //---use the LocationManager class to obtain GPS locations---
        lm = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new MyLocationListener();

        lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);    

        Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (loc != null) {
            tv.setText("Hello, lat is " + loc.getLatitude() + " lon is " + loc.getLongitude());
            setContentView(tv);
        } else {
            tv.setText("Hello, loc is null :(");
            setContentView(tv);
        }
    }

    private class MyLocationListener implements LocationListener 
    {
       @Override
        public void onLocationChanged(Location loc) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
            // TODO Auto-generated method stub
        }
    }        

}

Solution

  • You're misunderstanding how location works in Android. getLastKnownLocation() doesn't provide null unless the GPS has a fix, it returns null if there's no previous known location - the location does not have to come from the GPS (it could come via wifi or mobile network) and calling that method does not instruct the location framework to return a new location.

    You need to start again, after fully reading and understanding this page: http://developer.android.com/guide/topics/location/obtaining-user-location.html

    It includes everything you need to handle locations in Android, and you need a framework that suits the needs of your application. If you want the location to persist (to reduce the use of the GPS) then you can save the location to preferences, and/or enable network location sending (not as accurate, but usually faster). For some apps you may want to grab a rough location quickly (using last-known, or the network location provider) and then update it when an accurate GPS location arrives. But the approach you use will vary by app.