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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…