How does Android determine if an app has “High Bat

2020-02-17 07:42发布

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 "Low battery use".

I'm wondering if there are any tips to using GPS while keeping the "Low battery use" label. Perhaps if you poll infrequently enough - or is it hardcoded to GPS = battery killer?

EDIT:

I understand that changing those parameters will conserve battery life. My question was more of whether Android will recognize these attempts at battery life conservation or will it simply brand my app as high power usage simply because it uses GPS.

3条回答
聊天终结者
2楼-- · 2020-02-17 08:10

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<String> 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
查看更多
地球回转人心会变
3楼-- · 2020-02-17 08:12

I used the Fused Location (and [link removed]) and after a whole day of getting the location once every minute, my application used about 4% of the battery.

(Previous link is now a parking lot that tries to install browser extension: http://kpbird.com/2013/06/fused-location-provider-example.html?m=1)

查看更多
来,给爷笑一个
4楼-- · 2020-02-17 08:22

Good question but repetitive. Yes, polling frequency does effect you battery life much. So the frequency of getting the location updates of the user should be tailored according to specific needs.

  1. Basically is you read the android documentation of requestLocationUpdates of LocationManager, it says:

    requestLocationUpdates (long minTime, float minDistance, Criteria criteria, PendingIntent intent)

    Choosing a sensible value for minTime is important to conserve battery life. Each location update requires power from GPS, WIFI, Cell and other radios. Select a minTime value as high as possible while still providing a reasonable user experience. If your application is not in the foreground and showing location to the user then your application should avoid using an active provider (such as NETWORK_PROVIDER or GPS_PROVIDER), but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes) or greater. If your application is in the foreground and showing location to the user then it is appropriate to select a faster update interval.

    The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life.

  2. Please read following for some good answers on knowing how it drains and what can be done to minimize the battery drain:

    Android Regular GPS Polling in Service, maximizing battery life

    What's the most battery-efficient approach of using LocationClient to periodically get updates?

    Good way of getting the user's location in Android

    Save battery power consumed by gps services in android

    Is location provider really a battery drain?

    Battery life if using GPS and background app ios/android

    Keeping a GPS service alive and optimizing battery life

Hope this helps.

Edit: I agree to @ioan. Now you can use Fused Location API to get the location easily and efficiently.

查看更多
登录 后发表回答