OnPause to stop GPS

UPDATE # 2. My initial question was resolved by the kindness of the two who answered. Apparently, I need to ask a new question that the received code during its launch does not stop GPS. But now there are no errors, so I would mark this as a solution if I knew how :-)

UPDATE

I have a “working” code thanks to the comments, the only problem is that it does not do what it should do. It works in that there are no errors.

To make it work, I added it globally at the top of the action.

    protected LocationListener ll;
protected LocationManager lm;

And used this code OnPause

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

. GPS-. , , GPS, . GPS . , , . , lm.removeUpdates(ll) , .

--------------- -------------------------

OnPause, GPS. , , stackoverflow , , Eclipse.

GPS ( ):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textLat = (TextView)findViewById(R.id.textLat);
    textLong = (TextView)findViewById(R.id.textLong);
    textTimex = (TextView)findViewById(R.id.textTimex);
    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)
            {
                double pLat = location.getLatitude();
                double pLong = location.getLongitude();
                long pTime = location.getTime();
                DecimalFormat df = new DecimalFormat("#.######");
                textLat.setText(df.format(pLat));
                textLong.setText(df.format(pLong));
                textTimex.setText(Long.toString(pTime));
                //textTimex.setText(Long.toString(pTime));
                //textTimex.setText(df.format(pLong));
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub
        } 
    }

, .

@Override
public void onPause() {
    lm.removeUpdates(locationListener);
    super.onPause();
}

, lm, , . , , .

- ...

0
2

locationListener ( onCreate Activity) :

    LocationListener ll;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ////your code...
       ll = new mylocationlistener();
       ////your code..
    }


   @Override protected void onPause() 
 { 
  super.onPause();
   // TODO Auto-generated method stub 
   System.out.println("a intrat in onPause()"); 
  if(lm != null) { 
    lm.removeUpdates(ll); 
  } 
  ll = null; 
  lm = null;  
}
+1

, , onCreate. -.

a:

protected LocationListener ll;
protected LocationManager lm; 

LocationListener ll ll onPause. lm LocationManager.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textLat = (TextView)findViewById(R.id.textLat);
    textLong = (TextView)findViewById(R.id.textLong);
    textTimex = (TextView)findViewById(R.id.textTimex);
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
0

All Articles