Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

android - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...