In my Android application I use GoogleApiClient
to work with the location service. Everything works well when I call requestLocationUpdates()
in the following way:
locationProvider.requestLocationUpdates(mGoogleApiClient, locationRequest, (LocationListener) thisFragment);
where mGoogleApiClient
is initialized in onCreate()
method of my activity:
private GoogleApiClient mGoogleApiClient;
[...]
// onCreate() method in my activity
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API, Plus.PlusOptions.builder().build())
.addApi(LocationServices.API)
.addScope(new Scope("email"))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
while locationProvider
and locationRequest
are initialized in onAttach()
method of my fragment that also implements com.google.android.gms.location.LocationListener
:
//onAttach() method in my fragment
this.locationProvider = LocationServices.FusedLocationApi;
this.locationRequest = new LocationRequest();
this.locationRequest
.setInterval(Constants.GOOGLE_LOCATION_INTERVAL)
.setFastestInterval(Constants.GOOGLE_FASTEST_LOCATION_INTERVAL)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
The problem is that, sometimes, a user can ask for retrieving her location in an indoor environment, so I'd like to terminate the requestLocationUpdates()
request after a certain amount of time.
So far, I've tried the following solutions, but without success:
1) Using a Looper and a Handler . Actually this solution worked well with the old LocationManager
.
Looper looper = Looper.myLooper();
Handler handler = new Handler(looper);
handler.postDelayed(new Runnable() {
@Override
public void run() {
locationProvider.requestLocationUpdates(mGoogleApiClient, locationRequest, (LocationListener) thisFragment, looper);
}
}, Constants.LOCATION_TIMEOUT_MS);
2) Using only the Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
locationProvider.requestLocationUpdates(mGoogleApiClient, locationRequest, (LocationListener) thisFragment);
}
}, Constants.LOCATION_TIMEOUT_MS);
However, in both the two cases, the timeout (expressed by Constants.LOCATION_TIMEOUT_MS
) never expires. The GPS service keeps working endlessly.
First of all those two codes are identical:
Looper looper = Looper.myLooper();
Handler handler = new Handler(looper);
and
Handler handler = new Handler();
if you check the source code you can see that the empty constructor for Handler internally uses Looper.myLooper();
I'm not sure where you think this could would be cancelling a request, after the time passes, you are calling requestLocationUpdates
again. So yes, it will keep trying to get GPS lock.
I'll suggest you two methods how you should be doing.
This first one is easier but I'm not sure how well it works, as I only used it for passive location updates. Use the expiration
on your request, after that expirated time, the location services should quit automatically.
locationRequest.setExpirationDuration(Constants.LOCATION_TIMEOUT_MS);
This second is to use the correct method, which is removeLocationUpdates
locationProvider.removeLocationUpdates( mGoogleApiClient, (LocationListener) thisFragment);
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.