lm.removeUpdate(ll); not releasing updates

2019-09-18 23:56发布

I have been working for 2 days looking for this bug. I have searched stackoverflow and Android documentation with no luck.

My onLocationChanged code has a counter in it that counts how many times it has been called and if I back arrow out of the activity screen on the phone and return, the counter will go up by 2 for each update. If I back arrowing out and update the GPS, the counter records that onLocationChanged is still getting called even though the screen is in the background and onPause has been called. If I go in and out of the activity with the backarrow, I can get more than two updates per GPS input send depending on how many times the activity screen is entered.

All the GPS code works but these multiple instances can't be good and they mess up other things I am trying to do, like distance traveled between two updates.

Here is what I think is the relevant parts of my code. Obviously I left out the main part but the point is that after returning to this screen after back-arrowing out then a single send of a GPS data point increments the n variable by more than one depending on how many times I have returned to the screen.

I must be doing something obvious but I can't find it.

public class Data extends Activity {


protected LocationListener ll;
protected LocationManager lm;
static int n = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}


    class mylocationlistener implements LocationListener{
        @Override
        public void onLocationChanged(Location location) {
            if (location != null){
                n = n + 1;
                                    textData.setText("\n" + n);
                            }
     }

and

        @Override
    protected void onPause() {
        if(lm != null) {
            lm.removeUpdates(ll);
        }
        ll = null;
        lm = null;
        super.onPause();
    }

The only clue I have is that if I take the lm.removeUpdates(ll) out of the if(lm != null) then the code crashes which makes me think that lm must be null and that lm.removeUpdates(ll) must not be correct but it matches the all the examples I could find as well as the Android documentation as far as I can tell.

Please help.

标签: android gps
1条回答
甜甜的少女心
2楼-- · 2019-09-19 00:52
  LocationListener ll = new mylocationlistener();

This LocationListener is local to your method onCreate().So is your LocationManager lm.So when you are removing updates its not working with the manager and listener that you declared as the class variable. Just write as

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener(); in your onCreate().
查看更多
登录 后发表回答