简体   繁体   中英

Location updates limit in background service in Android 8

I have a background service with location updates every 12 minutes. We want to test location updates every 5-6 minutes, but in Android documentation we found this:

In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently background apps can retrieve the user's current location. Apps can receive location updates only a few times each hour.

https://developer.android.com/about/versions/oreo/background-location-limits.html

Code in the service to receive updates:

    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            Location location = locationResult.getLastLocation();
            //...
        }
    };

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    mLocationRequest = new LocationRequest();

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(TIME_TO_CHECK_LOCATION);
    mLocationRequest.setFastestInterval(TIME_TO_CHECK_LOCATION);
    mLocationRequest.setMaxWaitTime(TIME_TO_CHECK_LOCATION);

    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);

Anyone have tested the limits of location updates in a background service using Android 8?

The limits are a fixed number of times per hour?

If you are using fused location provider, and if another app is requesting location in foreground, or it is using foreground service to request location, your app will get as much location as this app. If no other app is requesting Location, and you put your app in background, from my real device test, you can expect a new location every 3 mins on Samsung Galaxy S8(runs Android 8.0), every 5 minutes on Google Pixel(runs Android 8.1). Looks like this is not a very stable number. You can use foreground service to collect location, which has no limitation.

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