5

I think I’m doing this right?

I have this code which starts looking for my GPS location via a MyLocationListener method not displayed here, that works but I want to stop the locationManager onPause, I think or whenever this activity is not current, but I can’t get the removeUpdates code to resolve.

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” wont resolve, I’ve also tried “this” and,

locationManager.removeUpdates((LocationListener) this);

Which resolves but gives me a “Cannot Pause” error at runtime.

Darren Burgess
  • 4,200
  • 6
  • 27
  • 43

3 Answers3

8

I had a similar question: Having some trouble getting my GPS sensor to stop

you might need to define a LocationListener that is the same for the one that is started and the one that is ended.

Try:

LocationListener mlocListener; 

under the class name and then use the following in your onCreate method:

mlocListener = new MyLocationListener();

And use the above mlocListener for the whole class.

So have your class look something like this:

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();
} 
Community
  • 1
  • 1
Stagleton
  • 1,060
  • 3
  • 11
  • 35
  • Thanks Stagleton, I was very close, eclipse was telling me in it’s own way I needed to declare a class level variable and I almost had it. It came down to a combination of me trying to learn hard to test stuff at the same time as learning Android. Thanks again! –  Dec 18 '11 at 01:53
  • cheers! Happy to hear it worked. Yes, I need to get better at reading the logcat myself – Stagleton Dec 18 '11 at 09:49
  • Would have been nice if Stagleton wrote a LocationManager as a `Singleton`class in his example ;) – Oritm Aug 13 '14 at 12:09
4

You can implement the LocationListener interface in your Activity class:

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);
    }
}
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Pygmalion
  • 561
  • 5
  • 16
3

I think 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();
}
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66