Android: requestLocationUpdates updates location at most every 45 seconds

后端 未结 3 1302
無奈伤痛
無奈伤痛 2020-12-13 05:08

Background

I am writing an Android app whose main function is tracking the user\'s location and making an alert when the user gets near some point. Therefore I nee

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 05:54

    You are completely right, the minimum time 45 seconds is harcoded in Android.

    This seems to be a NetworkLocationProvider class source code, when it was still in Android core:

    http://www.netmite.com/android/mydroid/frameworks/base/location/java/com/android/internal/location/NetworkLocationProvider.java

    Look at the variable:

    private static final long MIN_TIME_BETWEEN_WIFI_REPORTS = 45 * 1000; // 45 seconds
    

    And the method:

    @Override
    public void setMinTime(long minTime) {
        if (minTime < MIN_TIME_BETWEEN_WIFI_REPORTS) {
            mWifiScanFrequency = MIN_TIME_BETWEEN_WIFI_REPORTS;
        } else {
            mWifiScanFrequency = minTime;
        }
        super.setMinTime(minTime);
    }
    

    Now NetworkLocationProvider is out of the Android core, you can find it in NetworkLocation.apk in /system/app

    You can find an explanation of why is out of the core here:

    https://groups.google.com/forum/?fromgroups=#!topic/android-platform/10Yr0r2myGA

    But 45 seconds min time seems to still be there. Look at this NetworkProvider decompilation:

    http://android.fjfalcon.com/xt720/miui-trans/apk-decompiled/NetworkLocation/smali/com/google/android/location/NetworkLocationProvider.smali

    .line 149
    const-wide/32 v4, 0xafc8
    
    iput-wide v4, p0, Lcom/google/android/location/NetworkLocationProvider;->mWifiScanFrequency:J
    

    As you might guess if you convert 0xafc8 to decimal you get 45000 milliseconds

    I haven't found an explanation of why 45 seconds. I suppose there will be reasons like avoiding service overloading or other uses they don't want.

    In fact, there is a 100 request courtesy limit to Geolocation API:

    https://developers.google.com/maps/documentation/business/geolocation/#usage_limits

    But they don't seem to respect this rule in Google Maps app. If you open it and you only active network location you can notice that yout location is updated much more frequently than 45 seconds.

    I noticed this line suspiciously frequent (33 times a second) in logcat when Google Maps is open:

    02-20 17:12:08.204: V/LocationManagerService(1733): getAllProviders

    I guess Google Maps is also calling removeUpdates() and requestLocationUpdates() again to obtain a new position.

    So I think there is no fix and this is the best you can do if you want to get network locations over one in 45 seconds.

提交回复
热议问题