Battery Optimization (wakelocks) on Huawei EMUI 4.0+

Good afternoon, situation:

I am developing an Android application that serves as a sports tracker / navigation application, so it requires a constant connection to GPS, as well as constant wakefulness. Recording is performed every second.

The current solution, working for many years, thanks to wakelocks that the device is not working.

Doze mode in Android 6.0+ complicates the situation, but that's not the point.

There is probably another type of optimization on the Huawei device.

Here is the part of the magazine:

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, the system will simply decide that the application uses too many wakelocks and completely terminates it with all services, history, just kill .

Any experience with this behavior and any suggestion, how is this simple task (constantly recording GPS location with the screen off) better?

As I wrote at the beginning, on all other devices except the new Huawei, such a system has been working correctly for many years.

EDIT : Note after one user’s comment (deleted?), The whitelist application in Huawei Battery Manager (mark as a “secure application”) does not affect this problem.

+6
android huawei gps wakelock
source share
2 answers

There are two Huawei system apps that can kill custom apps to save battery:

  • SystemManager ( com.huawei.systemmanager ) kills all applications that still work after turning off the screen if they are not in the Protected Applications list.
  • PowerGenie ( com.huawei.powergenie ) kills any applications that hold a wakeful lock for a long time.

It looks like your application is killed by PowerGenie. You can avoid this by using PowerGenie hard coding with tracking lock shortcuts. For example, if your tracking lock tag is “LocationManagerService”, it will be ignored by PowerGenie because the system service contains a tracking lock with the same tag, and PowerGenie has a white list.

+4
source share

Have you tried to set an alarm that regularly releases WakeLock and then restarts it in a couple of seconds? If the problem is that Huawei Android does not like to abuse Wakelocks, maybe they are fine if you release it from time to time? For example: I assume that there will be a background process in the foreground. If so, add to your onStartCommand:

 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 recipient 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 ); } } 

Please note that in my case, I also had a background service running in the foreground, and I decided to stop the foreground. Not sure if this is necessary.

Of course, the risk is that within 3 seconds Wakelock is not active, your process may be killed.

+1
source share

All Articles