Skip to content Skip to sidebar Skip to footer

Having Some Trouble Getting My Gps Sensor To Stop

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

Solution 1:

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

Solution 2:

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

Solution 3:

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

@OverridepublicvoidonDestroy(){
mlocManager.removeUpdates(mlocListener);

    super.onDestroy();
} 

@OverridepublicvoidonResume(){

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

}

Post a Comment for "Having Some Trouble Getting My Gps Sensor To Stop"