When my application stops working, my GPS sensor continues to work, or when I change the actions due to which my application crashes depending on my configuration (the code below crashes when changing actions or exiting the application). How can I change my activity below so that when I exit the application the GPS sensor stops working, but when I switch to another action, it continues to work?
Here are the main components of my activity:
public class InitialChoice extends Activity {
LocationManager mlocManager;
LocationListener mlocListener;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_screen);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
@Override
protected void onStop(){
mlocManager.removeGpsStatusListener((Listener) mlocListener);
mlocManager = null;
}
}
If I use the following code instead of onStop (), gps will stop working:
@Override
public void finish(){
mlocListener = null;
mlocManager = null;
}
LogCat:
12-16 21: 54: 58.120: D / LocationManager (909): requestLocationUpdates: provider = gps, listener = com.dummies.android.taskreminder.MyLocationListener@46284458
12-16 21: 55: 04.740: D/AndroidRuntime (909):
12-16 21: 55: 04.740: W/dalvikvm (909): threadid = 1: ( = 0x400259f8)
12-16 21: 55: 04.750: E/AndroidRuntime (909): FATAL EXCEPTION: main
12-16 21: 55: 04.750: E/AndroidRuntime (909): java.lang.RuntimeException: {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice}: java. lang.ClassCastException: com.dummies.android.taskreminder.MyLocationListener
12-16 21: 55: 04.750: E/AndroidRuntime (909): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632)
12-16 21: 55: 04.750: E/AndroidRuntime (909): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677)
12-16 21: 55: 04.750: E/AndroidRuntime (909): at android.app.ActivityThread.access $2600 (ActivityThread.java:135)
12-16 21: 55: 04.750: E/AndroidRuntime (909): at android.app.ActivityThread $H.handleMessage(ActivityThread.java:2153)
12-16 21: 55: 04.750: E/AndroidRuntime (909): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 21: 55: 04.750: E/AndroidRuntime (909): at android.os.Looper.loop(Looper.java:144)
12-16 21: 55: 04.750: E/AndroidRuntime (909): at android.app.ActivityThread.main(ActivityThread.java:4937)
12-16 21: 55: 04.750: E/AndroidRuntime (909): java.lang.reflect.Method.invokeNative( )
: Android
12-16 23: 10: 50.295: W/dalvikvm (1272): threadid = 1: ( = 0x400259f8)
12-16 23: 10: 50.305: E/AndroidRuntime (1272): FATAL EXCEPTION: main
12-16 23: 10: 50.305: E/AndroidRuntime (1272): java.lang.RuntimeException: {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice}: android. app.SuperNotCalledException: {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice} super.onStop()
12-16 23: 10: 50.305: E/AndroidRuntime (1272): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3632)
12-16 23: 10: 50.305: E/AndroidRuntime (1272): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3677)