Search code examples
androidservicegpslocationlocationlistener

RemoveUpdates of LocationListener is not working on service class


I am trying to remove locationListener after getting the location. I am using the same listenerOBj I request for the location updates but its not working. I have another question that the service is not stopped by calling serviceClassObject.stopself?

Thanks in advance.

Here is my code

public class GPSTracker extends Service implements LocationListener {


private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManagerGPs,locationManagerNet;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManagerGPs = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        locationManagerNet = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManagerGPs
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManagerNet
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        locationManagerGPs.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MIN_TIME_BW_UPDATES,
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

        locationManagerNet.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

         if (isGPSEnabled || isNetworkEnabled) {
            this.canGetLocation = true;
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {

                if (locationManagerGPs != null) {
                    location = locationManagerGPs
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();

                        Log.d("tttt","GPStracker taken complete");
                    }
                }
            }
            else{

                if (locationManagerNet != null) {
                    location = locationManagerNet
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();

                        Log.d("tttt","network taken complete");
                    }
                }
            }
        }else{
             this.canGetLocation = false;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }
    return longitude;
}
public void stopUsingGPS(){
    if(locationManagerGPs != null){
        Log.d("tttt","trying to stop GPStracker");
        locationManagerGPs.removeUpdates(this);
    }
    if(locationManagerNet != null){
        Log.d("tttt","trying to stop GPStracker net");
        locationManagerNet.removeUpdates(this);
    }
}

public boolean canGetLocation() {
    return this.canGetLocation;
}


@Override
public void onLocationChanged(Location location) {
    Log.d("tttt","onLocationChanged called on GPStracker");
    getLocation();
}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {
    Log.d("tttt","location provider is enabled");
    getLocation();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("tttt","onStatusChanged called on GPStracker");
}

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

}

And I called from activity

GPSTracker gps;
gps = new GPSTracker(HomeActivity.this);

if(gps.canGetLocation()){
lat = gps.getLatitude();
lng = gps.getLongitude();

gps.stopUsingGPS();
gps.stopSelf();
}

Solution

  • At last, I got the solution finally. The main problem was in the approach of using service. Just by extending a service class is not the right way to use a service. There are some steps which must be followed when we would need to do some task with the help of service.

    Step 1: After extending a service class we need to Override onStartCommand(Intent intent, int flags, int startId) and onDestroy() methods.

    Step 2: Add it to the manifest like

    Step 3: TO start the service write startService(new Intent(getBaseContext(), className.class));

    Step 4: When the task is complete stop it as stopService(new Intent(getBaseContext(), className.class));