Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Location Request, set expiration duration doesn't work

I'm using new API of location which had been introduced in I/O 2013.

It works fine and I have no problem with its results. My problem is when I set setExpirationDuration(WHATEVER_MILLIS) to whatever millis however it works for one minutes.

This is my code:

LocationRequest locationRequest = LocationRequest.create()
                            .setInterval(10 * 60 * 1000) // every 10 minutes
                            .setExpirationDuration(10 * 1000) // After 10 seconds
                            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

When I run the app, satellite indicator will be displayed in tray so, I expect to see it just 10 seconds. However, it will be disappear after a minute.

Any suggestion or comments would be appreciated. Thanks.

like image 205
Hesam Avatar asked Jun 04 '13 03:06

Hesam


2 Answers

Since it seems to be a real problem in Android, a workaround could be a Handler to remove location updates manually.

    listHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                mLocationClient.removeLocationUpdates(MyActivity.this);
                //Location Updates are now removed
            }
            super.handleMessage(msg);
        }
    };

After you request for location updates (for a maximum duration of 10s) you call this Handler, of course delayed for 10s.

myLocationClient.requestLocationUpdates(...);
listHandler.sendEmptyMessageDelayed(0, 10000);

This makes sure, that after 10s your location updates are removed.

like image 98
Sorcerer Avatar answered Sep 19 '22 12:09

Sorcerer


In the documentation (https://developer.android.com/reference/com/google/android/gms/location/LocationRequest.html#setExpirationDuration(long))

It says that: "The duration begins immediately (and not when the request is passed to the location client), so call this method again if the request is re-used at a later time."

One would expect this duration to start when following line is called:

LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, this);

But, documentation states this duration begins when request object is created.

like image 27
Singed Avatar answered Sep 22 '22 12:09

Singed