I have been working for 2 days looking for this error. I searched for stackoverflow and Android documentation with no luck.
My onLocationChanged code has a counter that counts how many times it was called, and if I return the arrow from the activity screen on the phone and return, the counter will increase by 2 for each update. If I go back and update the GPS, the counter records that onLocationChanged is still making a call, even if the screen is in the background and onPause is being called. If I log in and log out of activity using backarrow, I can get more than two updates for each GPS input, depending on how many times the activity screen is entered.
All GPS code works, but these few instances may not be good, and they messed up the other things I'm trying to do, like the distance traveled between two updates.
This is what I consider relevant parts of my code. Obviously, I left the main part, but the fact is that after returning to this screen after turning back, one sending the GPS data point increases the n variable by more than one, depending on how many times I return to the screen.
I have to do something obvious, but I cannot 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();
}
, , , lm.removeUpdates(ll) if (lm!= null), , , lm lm.removeUpdates(ll ) , , , Android, .
, .