1

I have the following code, but it always return location = null, so can anyone help ?

public class GPSLocationService extends Service implements LocationListener {

    private LocationManager     mLocationManager;
    private boolean             mEnabled;
    private Criteria            mCriteria;
    private String              mProvider;
    private Location            mLocation;
    private Timer               mTimer;
    private LocationInfo        mLocationInfo;

    @Override
    public void onCreate() {

        super.onCreate();
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        mTimer = new Timer();

        if (mEnabled == false) {
            PhoneUtils.openGPSSettings(getApplicationContext());
        } else {

            mTimer.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {

                    mCriteria = new Criteria();
                    mProvider = mLocationManager.getBestProvider(mCriteria, false);
                    mLocation = mLocationManager.getLastKnownLocation(mProvider);
                    if (mLocation != null) {

                        onLocationChanged(mLocation);
                    } else {

                        onLocationChanged(null);
                    }

                }
            }, 0, 5000);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {

            double lat = location.getLatitude();
            double lng = location.getLongitude();

            mLocationInfo = new LocationInfo();
            mLocationInfo.setLatitude(lat);
            mLocationInfo.setLongitude(lng);
            ItamenDataController.getInstance().setLocationInfo(mLocationInfo);
        } else {

            mLocationInfo = new LocationInfo();
            mLocationInfo.setLatitude(-1);
            mLocationInfo.setLongitude(-1);
            ItamenDataController.getInstance().setLocationInfo(mLocationInfo);
        }
    }

    @Override
    public void onProviderDisabled(String provider) {

        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

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

    }
}
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175
  • Did you see this - http://stackoverflow.com/questions/9873190/my-current-location-always-returns-null-how-can-i-fix-this ? – Sergey Shustikov Jul 03 '14 at 13:32
  • Please see my answer here. http://stackoverflow.com/questions/24598609/gps-coordinates-using-location-manager-get-printed-as-null/24600264#24600264 – SeahawksRdaBest Jul 06 '14 at 21:51

1 Answers1

0

You're using LocationManager wrong. getLastKnownLocation returns last known location which can be out of date. You should use requestLocationUpdates to receive location updates.

holtaf
  • 787
  • 6
  • 19