简体   繁体   中英

Unable to Stop Android location updates after activity gets destroyed by configuration change

I am building an application that uses plays services API to get the location. I start the location updates with the next command:

LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

To stop it, I just execute the next piece of code which is executed when a "STOP" button is pressed:

LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);

Nevertheless if the configuration changes (for example, rotating the screen). When I press the button, the application goes to an error:

java.lang.IllegalStateException: GoogleApiClient is not connected yet.

Can you tell me, how can I stop the location updates with my "Stop" button after the screen has been rotated? Thanks a lot!

I dont want to DISABLE the configuration change when the screen gets rotated.

I think that when you rotate a screen activity refresh itself. To handle this error try to use:

if (!mGoogleApiClient.isConnecting())
 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

or just:

if (mGoogleApiClient.isConnected())
 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

停止并重新启动onPause()和onResume()方法中的更新,如开发人员指南建议的那样: http : //developer.android.com/training/location/receive-location-updates.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM