Having some trouble getting my GPS sensor to stop

2019-08-02 09:37发布

问题:

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)

回答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



回答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



回答3:

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();
}

}


标签: android gps