0

I am not getting GPS location in my code, while Google maps is showing current location and even its updating, I am starting a service, inside service registering locationListener to the locationManager, handling onLocationChanged callback method, doing entry in the AndroidManifest.xml even. The logs onCreate methods is showing.
below is my code, could you guys please let me know where i am doing wrong...

public class LocationService extends Service implements LocationListener {
private static final long MIN_TIME_INTERVAL_FOR_GPS_LOCATION = 100;
private static final float MIN_DISTANCE_INTERVAL_FOR_GPS_LOCATION = 1.0f;
private static String TAG = LocationService.class.getSimpleName();
private LocationManager locationManager;
private static Location mCurrentLocation;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "onCreate...");

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_INTERVAL_FOR_GPS_LOCATION, MIN_DISTANCE_INTERVAL_FOR_GPS_LOCATION, this);
    mCurrentLocation = getBestLocation();
}

private Location getBestLocation() {
    Log.i(TAG, "getBestLocation...");
    Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    // If both are available, get the most recent
    if (location_gps != null && location_network != null) {
        return (location_gps.getTime() > location_network.getTime()) ? location_gps : location_network;
    } else if (location_gps == null && location_network == null) {
        return null;
    } else {
        return (location_gps == null) ? location_network : location_gps;
    }
}

@Override
public void onLocationChanged(Location location) {
    Log.i(TAG, "onLocationChanged...");
    Toast.makeText(LocationService.this, "Location Found", Toast.LENGTH_SHORT).show();
    mCurrentLocation = location;
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

public static Location getmCurrentLocation() {
    return mCurrentLocation;
}

public static void setmCurrentLocation(Location mCurrentLocation) {
    LocationService.mCurrentLocation = mCurrentLocation;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "onDestroy...");
    locationManager.removeUpdates(this);
}
}
umesh
  • 1,148
  • 1
  • 12
  • 25

1 Answers1

1

Your locationManager called requestLocationUpdates using GPS provider only.

I think your device could not catch GPS satellite signal, in-door sate.
So, you should call requestLocationUpdates using Network provider too.

Next two links are may helpful to you.
http://developer.android.com/guide/topics/location/strategies.html
http://developer.android.com/training/location/receive-location-updates.html

Kyoung-june Yi
  • 503
  • 1
  • 3
  • 16
  • so you are saying that i can register locationListener for other provider too at the same time ? – umesh Aug 07 '14 at 06:37
  • Yes.. class LocationService extends Service { LocationListener gpsListener = new Location Listener { } LocationListener networkListener = new Location Listener { } } – Kyoung-june Yi Aug 07 '14 at 06:46
  • Check this [link](http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/3145655#3145655) – Kyoung-june Yi Aug 07 '14 at 06:49