Search code examples
androidgpslocationmanagerlocationlistener

GPS System wont restart after calling "removeUpdates"


In my application I call "requestLocationUpdates" in onStart() method and I receive updates properly.

When I turn the GPS off by calling "removeUpdate(locationlistener)" I stop receiving updates as expected.

The problem is that when I want to restart the GPS system by calling "requestLocationUpdates" method again it isn't working! It never enters the "onLocationChanged" or "onStatusChanged" methods

my gpsLocationListener code;

public final LocationListener gpsLocationListener = new LocationListener() {
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
        case LocationProvider.AVAILABLE:
            Logging.TraceMessage("GPS available again", Logging.INFORMATION, Logging.MAINACTIVITY);
            break;
        case LocationProvider.OUT_OF_SERVICE:
            Logging.TraceMessage("GPS out of service", Logging.INFORMATION, Logging.MAINACTIVITY);
            deviceSettings.gpsStatus = "2";
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            Logging.TraceMessage("GPS temporarily unavailable", Logging.INFORMATION, Logging.MAINACTIVITY);
            break;
        }
    }

    public void onProviderEnabled(String provider) {
        Logging.TraceMessage("GPS Provider Enabled", Logging.INFORMATION, Logging.MAINACTIVITY);
    }

    public void onProviderDisabled(String provider) {
        Logging.TraceMessage("GPS Provider Disabled", Logging.INFORMATION, Logging.MAINACTIVITY);
        deviceSettings.gpsStatus = "2";
    }

    public void onLocationChanged(Location location) {
        if(location == null) return;

        tempLoc.setItem(location.getLongitude(), location.getLatitude(), deviceSettings.getCurrentTime(), deviceSettings.satelliteCount, location.getSpeed());
            if (!SafeLocation.IsSafe(tempLoc) ){
            return;
        }

        deviceSettings.currX = tempLoc._x;
        deviceSettings.currY = tempLoc._y;
        deviceSettings.currSpeed = tempLoc._speed;
        deviceSettings.currValid = true;

    }
};

my removeupdates code;

if( ! deviceSettings.isprogramModeActive ){

                         if(gpsLocationListener != null){
                             try {
                                 locationManager.removeUpdates(gpsLocationListener);
                             } catch (Exception e) {
                             }                           
                         }
                         mylocatewait(30000);//30 saniye bekle
                         continue;
                    }

my request again code:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, ServerSettings.getGpsPer()*1000, 0, gpsLocationListener);

Solution

  • I solve the problem!

    I crate an call a handler from thread.

    locateHandler.sendEmptyMessage(1);
    

    I call the following code from a handler, not in the thread.

     Handler locateHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0)
                    locationManager.removeUpdates(gpsLocationListener);
                else if(msg.what == 1)
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, ServerSettings.getGpsPer()*1000, 0, gpsLocationListener);
            }
        };