即使使用dontkillmyapp.com的解决方案,华为设备也杀死了我的前台服务 [英] Huawei device killing my foreground service, even with dontkillmyapp.com's solution

查看:668
本文介绍了即使使用dontkillmyapp.com的解决方案,华为设备也杀死了我的前台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基本上是位置跟踪软件的应用.启动它时,它会保存位置并将其发送到服务器.

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.

该代码已经工作了大约5年,没有任何修改,没有任何错误.

它是通过简单的前台服务实现的.

最近几个月,我收到用户报告的关于服务在华为设备上随机停止的错误.首先,我认为这是新型Android设备上的罕见/新崩溃,但有 Fabric中根本没有错误日志.

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.

我在新的华为设备中尝试过,令我惊讶的是,这种现象确实存在. 几分钟后,华为设备(带有EMUI)确实杀死了前台服务.

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.

这真的对我的应用程序不利,首先,用户希望长时间运行此跟踪应用程序,其次,近几个月来,华为已成为Android用户中的热门选择.大概有10%的用户拥有华为设备.

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.

我知道 https://dontkillmyapp.com/这是一个获取有关以下信息的好网站这个问题.

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

我已经尝试了他们的解决方案-基本上是在我的服务中添加带有特定标签的唤醒锁,因此华为的EMUI不会杀死它.

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.

我的服务中的代码:

我基本上是在服务的onCreate回调中获取唤醒锁的.

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:

说明:我的服务是前台服务,带有永久性通知. 它可以在其他设备上的DAYS上很好地运行.

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

如果可以的话,请帮助

亚当

推荐答案

几个月前,我遇到了类似的问题,我花了很多时间来寻找解决方案,最后我发现了这个问题(我不知道是否为您工作,但这对我有所帮助.

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).

我在自己开发的应用程序中实现了该服务,该服务每分钟恢复一次用户位置,该位置保存在设备的sqlite数据库中.我需要应用程序始终不中断地工作.

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).

在我的办公桌上握了几拳之后,我意识到问题与节能选项有关,这对于某些制造商来说是不同的,但对于Android版本而言,是不同的.

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.

此解决方案对我有帮助

@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();
        }
    }
}

基本上,我要求用户(我无法通过其他任何方式)允许我的应用程序避免被优化(此代码适用于Build.VERSION.SDK_INT> = 23)

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)

清单文件需要此权限:

android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS  

这篇关于即使使用dontkillmyapp.com的解决方案,华为设备也杀死了我的前台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆