GPS not update location after close and reopen app on android
A few things...
What's probably happening is your app is being resumed and not created the "second" time. If you want it to look for a new location every time the activity is viewed you'll want to move the following code into the onResume()
method:
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener); }
You'll also want to make sure you unregister your LocationListener in the onPause()
method. Something like this should do it:
@Override
public void onPause() {
lm.removeUpdates(locationListener);
super.onPause();
}
It looks like you have a typo on this line:
locationListener = new mLocationListener();
I think it should be:
locationListener = new myLocationListener();
Also, as someone else mentioned, there's no reason to create a whole new class to act as your LocationListener. In fact, it'll burn up memory unnecessarily and on mobile devices memory is at a premium.
The combination of all of the things above would leave your Activity looking something like this:
public class Whatever extends Activity implements LocationListener {
private LocationManager lm;
private LocationListener locationListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new mLocationListener();
}
@Override
public void onResume() {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
super.onResume();
}
@Override
public void onPause() {
lm.removeUpdates(this);
super.onPause();
}
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
TextView gpsloc = (TextView) findViewById(R.id.widget28);
gpsloc.setText("Lat:"+loc.getLatitude()+" Lng:"+ loc.getLongitude());
}
}
@Override
public void onProviderDisabled(String provider) {
TextView gpsloc = (TextView) findViewById(R.id.widget28);
gpsloc.setText("GPS OFFLINE.");
}
@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
}
}