OnPause to Stop LocationManager

I think I'm doing it right?

I have this code that starts searching for my GPS location using the MyLocationListener method, which does not appear here, that works, but I want to stop the locationManager onPause, I think, or when this action is not current, but I cannot get the removeUpdates code for solutions.

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);        
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());

and then

@Override 
public void onPause()
{
    super.onPause();
    locationManager.removeUpdates(MyLocationListener);
}

"MyLocationListener" will not allow, Ive also tried "this" and,

locationManager.removeUpdates((LocationListener) this);

Which solves, but gives me a "Can not Pause" error at runtime.

+4
source share
3 answers

I had a similar question: Some problems with stopping my GPS sensor

LocationListener, , , , .

Try:

LocationListener mlocListener; 

, onCreate:

mlocListener = new MyLocationListener();

mlocListener .

, :

public class SomeClass extends Activity {
    LocationManager mlocManager;
    LocationListener mlocListener; 

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screenlayout);
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}

@Override
public void onPause(){
    mlocManager.removeUpdates(mlocListener);
    super.onPause();
} 
+8

LocationListener Activity:

public class MyActivity extends Activity implements LocationListener {

    private LocationManager mLocMgr;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, this);

    }

    @Override
    public void onLocationChanged(Location location) {}
    @Override
    public void onProviderDisabled(String arg0) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

    @Override
    public void onPause() {
        super.onPause();
        mLocMgr.removeUpdates(this);
    }
}
+4

It seems to me that you just need to switch the order of the super onPause call and the removeUpdates call.

@Override 
public void onPause()
{
    locationManager.removeUpdates(MyLocationListener);
    super.onPause();
}
+2
source

All Articles