1

When my application is quit my GPS sensor keeps working or when I change activities my application crashes depending on my configuration (in the code below it crashes when I change activities or quit the application). How do I change my below activity so that when I quit the application the GPS sensor stops running but when I move to another activity it keeps running?

Here is the key parts to 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.removeUpdates(mlocListener); 

        mlocManager.removeGpsStatusListener((Listener) mlocListener);

        mlocManager = null;
    }
}

If I use the following code instead of onStop() the gps wont stop running:

@Override
public void finish(){
    //mlocManager.removeUpdates(mlocListener); 

    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): Shutting down VM 12-16 21:55:04.740: W/dalvikvm(909): threadid=1: thread exiting with uncaught exception (group=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: Unable to stop activity {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): at java.lang.reflect.Method.invokeNative(Native Method)

New Errors for me with method from here: Stop Location Listener in Android

12-16 23:10:50.295: W/dalvikvm(1272): threadid=1: thread exiting with uncaught exception (group=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: Unable to stop activity {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice}: android.app.SuperNotCalledException: Activity {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.InitialChoice} did not call through to 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)

Community
  • 1
  • 1
Stagleton
  • 1,060
  • 3
  • 11
  • 35

3 Answers3

2

Your casting to a listener when its a locationlistener:

Declaration:

 LocationListener mlocListener; 

instantiation:

 mlocListener = new MyLocationListener();

cast:

 (Listener) mlocListener

Yout Logcat explains it here:

java.lang.ClassCastException: com.dummies.android.taskreminder.MyLocationListener 12-16

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • ok, but the method requires a listener and there is an error if I don't recast it. Is there a different method I can use that will accept a LocationListener? – Stagleton Dec 16 '11 at 21:59
  • I tried this, but it keeps crashing: http://stackoverflow.com/questions/6894234/stop-location-listener-in-android – Stagleton Dec 16 '11 at 22:08
  • Use removeUpdates then what does your logcat say. You really need to get used to reading your logcat to debug your errors instead of justs saying "it crashes" – Blundell Dec 16 '11 at 22:11
  • Will do, also LogCat info added above. Activity unable to stop? – Stagleton Dec 16 '11 at 22:16
  • 1
    just call super.onStop(); in the last line of your onStop() method, the logcat says it all! – Blundell Dec 16 '11 at 23:50
  • yes, I need to get better at reading it. I added some code above but the sensor stops listening when I leave the main activity. Can you think of a better way to keep it running until I leave the application aside from adding the above code to all my activities? – Stagleton Dec 17 '11 at 16:19
  • Thats a whole different question. Mark this is answered and start a new one. do NOT edit your question to change it completely. This answer is not just for you it's for other people coming across the same problem. – Blundell Dec 18 '11 at 15:36
  • My question was how do I get something to work. I will accept my answer when stackoverflow will allow me to. – Stagleton Dec 18 '11 at 18:25
1

try overriding finish() instead of `onStop()' for starters

another thing I've done is have the gpslistener be set to null after getting a position in onLocationChanged(), and finally, if you need to continue getting location updates then you should consider creating a service, instead of doing the gps listener in an activity.

that way you can kill the service easier, which will kill the listener

CQM
  • 42,592
  • 75
  • 224
  • 366
  • I have changed the 'onStop' method to: @Override public void finish(){ //mlocManager.removeUpdates(mlocListener); mlocListener = null; mlocManager = null; } But the GPS sensor stays on now – Stagleton Dec 16 '11 at 21:00
  • Wow don't override finish either override onPause or onStop , according to where in the activity lifecycle you want to stop listening. Overriding finish is only for when you explicitly finish your activity, not for when switching activitys like most cases. – Blundell Dec 16 '11 at 21:17
  • @Blundell, the documentation warns against using onStop since it may not always be called. onPause is better. he said he was trying to stop if when he quits the application, not just switching activity. finish() would be appropriate – CQM Dec 16 '11 at 21:44
  • @CQM finish() is not called when the activity is quit using the back button. Agreed he should use onPause – Blundell Dec 16 '11 at 22:12
  • I added some code above but the sensor stops listening when I leave the main activity. Can you think of a better way to keep it running until I leave the application aside from adding the above code to all my activities? – Stagleton Dec 17 '11 at 16:19
1

For now this seems to work reasonably well but there is probably a better way (background thread.....):

@Override
public void onDestroy(){
mlocManager.removeUpdates(mlocListener);

    super.onDestroy();
} 

@Override
public void onResume(){

    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    super.onResume();
}

}
Stagleton
  • 1,060
  • 3
  • 11
  • 35