I've written a Small Google Maps Application, which makes me a few Problems.
The LocationListener i wrote, overrides the onProviderDisabled Method to call an Activity, where the User can Switch on his GPS.
This do the Job well, but one Problem is still there. If i close the Maps_Activity and after that shut down the GPS, it seems, that the Listener is already running, because the Activity (the one who says me, that the GPS is not running) still pops up.
The LocationListner as followed:
public class LocationUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
mMyLocationOverlay.enableMyLocation();
mMyLocationOverlay.runOnFirstFix(new Runnable() {
@Override
public void run() {
mMapView.getController().animateTo(
mMyLocationOverlay.getMyLocation());
}
});
}
@Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(MapActivity.this, NoGpsActivity.class);
startActivity(i);
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS ready", Toast.LENGTH_LONG).show();
}
}
The NoGPSAcivity.class as followed:
public class NoGpsActivity extends Activity {
private OnClickListener mEnableGPSListener = new OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps_not_available_overlay);
Button mEnableGPS = (Button)findViewById(R.id.enablegps_button);
mEnableGPS.setOnClickListener(mEnableGPSListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 0){
finish();
}
}
}
Thank you for your help!
In your onDestroy() method you need to call removeUpdates(locationListener) so it will stop receiving updates.
Also another thing to watch out for is that the "Home" key in Android generally means "minimize" (leaving your program in the background) while the "Back" key means "close". So if your hitting home to exit, most likely it is still running.