I cannot get mlocManager.removeUpdates(mlocListener); to work

Posted by Colin on Stack Overflow See other posts from Stack Overflow or by Colin
Published on 2011-02-01T13:29:49Z Indexed on 2012/10/16 17:02 UTC
Read the original article Hit count: 160

Filed under:
|

I have an app that uses the LocationManage functions which works well until the app is stopped or paused. The location listener function is still carrying on in the background. Relevant bits of code follow. When I click home or back the onstop() function is being triggered correctly.

package uk.cr.anchor;

import android.app.Activity;
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.content.SharedPreferences; 
import android.graphics.Color;

public class main extends Activity {

    /** Called when the activity is first created. */


    private LocationManager mlocManager; 
    private LocationListener mlocListener; 

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


        stoplistening();
        super.onStop();
    }

/* Class My Location Listener */ 

public class MyLocationListener implements LocationListener 
{ 


    @Override 

    public void onLocationChanged(Location loc) 
    { 

        loc.getLatitude(); 

        loc.getLongitude(); 

        etc etc etc
    }
    private void stoplistening() {
        if (mlocManager != null) {
            Toast.makeText( getApplicationContext(), 
                "kill", 
                Toast.LENGTH_SHORT ).show(); 
            mlocManager.removeUpdates(mlocListener);
        }
        else {
            Toast.makeText( getApplicationContext(), 
                " not kill", 
                Toast.LENGTH_SHORT ).show();    
        }
    }
}

I always get the "not kill" message.

Can anyone help me!

© Stack Overflow or respective owner

Related posts about android

Related posts about locationlistener