Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huawei device killing my foreground service, even with dontkillmyapp.com's solution

I'm developing an app which is basically a location tracking software. When you start it, it saves locations and sending them to a server.

The code is working for like 5 years now without any modification, without any errors.

It is implemented with a simple foreground service.

In the recent months I was getting user reported errors about the service stops randomly on Huawei devices. First I thought it is some kind of rare/new crash on newer androids but there was no error logs at all in Fabric.

I tried it in a new Huawei device and for my greatest surprise, this phenomenon does really exists. Huawei devices (with EMUI) does really kills the foreground services after a couple of minutes.

This is really really bad for my app, first of all, the users want to run this tracking app for long hours, and secondly, the recent months made Huawei be a popular choice amongs Android users. Like 10% of my user base has a Huawei device.

I'm aware of https://dontkillmyapp.com/ It is a great website for getting information about this issue.

I have tried their solution - which is basically adding a wakelock with a specific tag to my service, so Huawei's EMUI won't kill it.

I've tried this in the following way, but my Huawei test device still kills my foreground service after some minutes.

Code inside my Service:

I basically aquires a wakelock in the service's onCreate callback.

 private void acquireLock() {      if (wakeLock == null) {         PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);         if (mgr != null) {              if (Build.MANUFACTURER.toLowerCase().equals("huawei")) {                 lockTag = "LocationManagerService";             }              wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, lockTag);              Log.i("MY_TAG", "tag:" + lockTag);         }     }     if (wakeLock != null && !wakeLock.isHeld()) {         wakeLock.acquire();         //also tried with: wakeLock.acquire(1000*60*60*72); 3 days wakelock just in case.         Log.i("MY_TAG", "wakeLock acquired!");     } }  @Override public void onCreate() {     acquireLock(); } 

E D I T:

Clraification: My service is a foreground service, with a presistent notification. It can run well for DAYS on other devices.

Please help if you can,

Adam

like image 757
Adam Varhegyi Avatar asked Jul 04 '19 07:07

Adam Varhegyi


People also ask

How to stop huawei killing apps?

BONUS: How to disable Huawei's closing of apps, for all apps If you are frustrated with how Huawei closes background apps when you lock the screen, you can disable this "feature" completely, for ALL your apps. To do that, in the "App launch"screen, tap the switch "Manage all automatically" and set it to disabled.

What is COM Huawei PowerGenie?

PowerGenie. Huawei is extremely inventive in breaking apps on their devices. In addition to all the non-standard power management measures described below, they introduced a new task killer app build right into EMUI 9 on Android Pie. It is called PowerGenie and it kills all apps that are not on its whitelist.


1 Answers

I had a similar problem a few months ago, I spent a lot of time to search a solution and finally i have found this (i don't know if works for you, but it was help me).

I implemented in an application that I developed, a service that recover the user position every minutes, this position is saved in the sqlite database of the device. I need that the application work alway without interrupt.

In the test phase I found that some devices interrupted the execution of my code (like in your case).

After several fists on my desk, I realized that the problem was related to energy saving options which are different for some manufacturers but also for Android versions.

This solution help me:

@SuppressLint({"NewApi", "BatteryLife"}) private void checkOptimization() {     String packageName = getApplicationContext().getPackageName();     PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);     if (pm != null) {         if (!pm.isIgnoringBatteryOptimizations(packageName)) {                 Intent intent = new Intent();                 intent.setAction(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);                 intent.setData(Uri.parse("package:" + ctx.getPackageName()));                 ctx.startActivity(intent);         } else {             new initialize().execute();         }     } } 

Basically I ask the user (I can't do it any other way) to allow my application to avoid being optimized (this code work for Build.VERSION.SDK_INT >= 23)

The manifest file needs this permission:

android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS   
like image 191
Mattia Avatar answered Oct 07 '22 17:10

Mattia