title: OnPause to stop GPS
body: <p>UPDATE #2.  My initial question was solved by the kindness of the two who answered.  Apparently I need to start a new question for the fact that the resulting code, while it runs, does not stop the GPS.  But there are no errors now so I would mark this as solved if I knew how :-)</p>
  
  <p>UPDATE.</p>
  
  <p>I have "working" code thanks to comments the only problem is that it doesn't do what it is supposed to do.  It is working in that there are no errors.</p>
  
  <p>To make it work, I added this globally at the top of the activity.</p>
  
  <pre><code>    protected LocationListener ll;
  protected LocationManager lm;
  </code></pre>
  
  <p>And used this OnPause code</p>
  
  <pre><code>    @Override
      protected void onPause() {
          if(lm != null) {
              lm.removeUpdates(ll);
          }
          ll = null;
          lm = null;
          super.onPause();
      }
  </code></pre>
  
  <p>Everything compiles and runs on a phone. I get GPS locations displayed.  Trouble is that the goal is to get the GPS to turn off so that the battery doesn't go dead after leaving the application.  But the GPS stays on.  I see this is a common complaint and have not seen an answer.  I thought lm.removeUpdates(ll) was the solution but it isn't working.</p>
  
  <p>--------------- original post -------------------------</p>
  
  <p>I want to add some OnPause code to stop my GPS updates.  I have tired at least a dozen examples of code from stackoverflow both questions and answers and all give errors in Eclipse.</p>
  
  <p>My GPS code is here (it works fine):</p>
  
  <pre><code>@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
          } 
      }
  </code></pre>
  
  <p>I am taking code from examples on the web and none of them work.  Here is one</p>
  
  <pre><code>@Override
  public void onPause() {
      lm.removeUpdates(locationListener);
      super.onPause();
  }
  </code></pre>
  
  <p>It really doesn't matter what I call lm, it gives an error that it cannot resolve it.  It doesn't matter which example code I use, none of them are error free.</p>
  
  <p>I must be doing something very simply stupid...</p>