How does Android determine if an app has “High Battery Use” under “Recent Location Requests”?

前端 未结 3 1797
谎友^
谎友^ 2020-12-24 08:48

As of Kitkat (4.4) Android reports that my app is \"High battery use\".

I use Network Location as well GPS. If I disable GPS, then it seems the app gets marked as \"

3条回答
  •  醉梦人生
    2020-12-24 09:42

    Recently refactored to obtain the location of the code, learn some good ideas, and finally achieved a relatively perfect library and Demo.

    //request all valid provider(network/gps)
    private boolean requestAllProviderUpdates() {
        checkRuntimeEnvironment();
        checkPermission();
    
        if (isRequesting) {
            EasyLog.d("Request location update is busy");
            return false;
        }
    
    
        long minTime = getCheckTimeInterval();
        float minDistance = getCheckMinDistance();
    
        if (mMapLocationListeners == null) {
            mMapLocationListeners = new HashMap<>();
        }
    
        mValidProviders = getValidProviders();
        if (mValidProviders == null || mValidProviders.isEmpty()) {
            throw new IllegalArgumentException("Not available provider.");
        }
    
        for (String provider : mValidProviders) {
            LocationListener locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    if (location == null) {
                        EasyLog.e("LocationListener callback location is null.");
                        return;
                    }
                    printf(location);
                    mLastProviderTimestamp = location.getTime();
    
                    if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                        finishResult(location);
                    } else {
                        doLocationResult(location);
                    }
    
                    removeProvider(location.getProvider());
                    if (isEmptyValidProviders()) {
                        requestTimeoutMsgInit();
                        removeUpdates();
                    }
                }
    
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                }
    
                @Override
                public void onProviderEnabled(String provider) {
                }
    
                @Override
                public void onProviderDisabled(String provider) {
                }
            };
            getLocationManager().requestLocationUpdates(provider, minTime, minDistance, locationListener);
            mMapLocationListeners.put(provider, locationListener);
            EasyLog.d("Location request %s provider update.", provider);
        }
        isRequesting = true;
        return true;
    }
    
    //remove request update
    public void removeUpdates() {
        checkRuntimeEnvironment();
    
        LocationManager locationManager = getLocationManager();
        if (mMapLocationListeners != null) {
            Set keys = mMapLocationListeners.keySet();
            for (String key : keys) {
                LocationListener locationListener = mMapLocationListeners.get(key);
                if (locationListener != null) {
                    locationManager.removeUpdates(locationListener);
                    EasyLog.d("Remove location update, provider is " + key);
                }
            }
            mMapLocationListeners.clear();
            isRequesting = false;
        }
    }
    

    Complete implementation: https://github.com/bingerz/FastLocation/blob/master/fastlocationlib/src/main/java/cn/bingerz/fastlocation/FastLocation.java

    Mark:

    • Each request to complete the location, it is best to removeUpdates, otherwise the phone status bar will always display the positioning icon

提交回复
热议问题