Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Battery optimizations (wakelocks) on Huawei EMUI 4.0+

Good day, situation:

I'm developing Android application that serve as sport tracker/navigation app - so it require permanent connection to GPS and also permanent awake device. Recording is done every second.

Current solution working for years is thanks to wakelocks that keep device awake.

Doze mode in Android 6.0+ complicate situation, but it is not this case.

On Huawei device is probably different type of optimization.

Here is part of log:

10-10 10:33:18.462 1014-384 D/PFW.HwPFWAppWakeLockPolicy: getUidWakeLock uid: 10097 wakelock >= 10 mins 10-10 10:33:18.543 1014-384 D/PFW.HwPFWAppWakeLockPolicy: force stop abnormal wakelock app uid: 10097 10-10 10:33:18.558 1014-384 I/ActivityManager: Force stopping menion.android.locus appid=10097 user=0: from pid 1014

So after approx. 30+ minutes, system simply decide that app use too much wakelocks and terminate it completely with all services, history, simply kill.

Any experience with this behavior and any suggestion, how to this simple task (permanent recording of GPS location when screen is off) better?

As I wrote at start, on all other devices except new Huawei, such system works correctly for many years.

EDIT: note after comment of one user (deleted?), "whitelist" app in Huawei battery manager (mark as "protected application") has no effect on this problem.

like image 664
Menion Asamm Avatar asked Oct 10 '16 09:10

Menion Asamm


People also ask

What are Wakelocks in Android?

A wake lock is a mechanism to indicate that your application needs to have the device stay on. Any application using a WakeLock must request the android. permission. WAKE_LOCK permission in an <uses-permission> element of the application's manifest. Obtain a wake lock by calling PowerManager#newWakeLock(int, String) .

What is power intensive prompt Huawei?

Huawei P10 and P10 Plus battery optimisation: Close power-intensive apps. One of EMUI's special features is that it constantly monitors active apps and how much power they're using. The software then prompts you via an alert to suggest closing said apps when they've been idly using energy.

What is battery optimization in Android?

Battery optimization helps conserve battery power on your device and is turned on by default. Devices running Android 6. x and higher include battery optimization features which improve battery life by placing apps in Doze mode or App Standby.


2 Answers

There are two Huawei system apps that may kill user apps to save battery:

  • SystemManager (com.huawei.systemmanager) kills any apps that are still running after the screen is turned off, unless they're in the "Protected Apps" list.
  • PowerGenie (com.huawei.powergenie) kills any apps that hold wake locks for a long time.

It sounds like your app is being killed by PowerGenie. You can avoid this by taking advantage of PowerGenie's hardcoded whitelist of wake lock tags. For example, if your wake lock's tag is "LocationManagerService" it will be ignored by PowerGenie, because a system service holds a wake lock with the same tag and PowerGenie has whitelisted it.

like image 155
akwizgran Avatar answered Sep 28 '22 02:09

akwizgran


Have you tried setting an Alarm that regularly releases the WakeLock and then reacquires it after a couple of seconds? If the issue is that Huawei's Android does not like the abuse of Wakelocks, maybe they are fine if you release it every now and then? For example: I suppose you will have a background process running in foreground. If so, in your onStartCommand insert:

setupWakeupAlarm(context);

where the method is defined as:

private static void setupWakeupAlarm(Context context) {
    AlarmManager mWakeUpAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent mWakeUpAlarmIntent;
    Intent serviceIntent;
    serviceIntent = new Intent(context, SSAlarmReceiver.class);
    mWakeUpAlarmIntent = PendingIntent.getBroadcast(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // every 5 minutes 
    mWakeUpAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + WAKEUP_ALARM_FREQUENCY, mWakeUpAlarmIntent);
    Log.d("TAG", "wakup alarm set up or reset!");
}

and where the receiver is a local class:

static public class SSAlarmReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        setupWakeupAlarm(context);
        mBackgroundService.stopForeground(true);
        if (mWakeLock.isHeld())
            mWakeLock.release();
        new Timer().schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        mWakeLock.acquire();
                        mBackgroundService.startForeground(mNotificationId, mNotification.getNotification());
                        }
                },
                3000
        );
    }
}

Note that in my case I also had my background service running in foreground and I decided to stop the foreground. Not sure it is necessary.

The risk is of course that in the 3 seconds the Wakelock is not active, your process can be killed.

like image 33
FabioC Avatar answered Sep 28 '22 03:09

FabioC