有没有办法连续运行服务? [英] Is there any way to run service continuously?

查看:29
本文介绍了有没有办法连续运行服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Stack Overflow 上有几个与此类似的问题,但没有一个解决方案对我有用

There are few questions similar to this on Stack Overflow but none of the solutions are working for me

问题只出现在少数设备上,例如 OnePlus 和 MI,一旦用户从最近的应用程序中滑动应用程序,该服务就会被终止.

The problem is with only few devices like OnePlus and MI, The service is getting killed as soon as the user swipes away app from recent app.

我读到这些 OEM 使用一些激进的策略来终止服务.我只是想知道有什么方法可以让服务保持运行或在它被杀死后立即启动它.

I read that these OEM'S use some aggressive strategies to kill services. I just want to know is there any way to keep service running or start it as soon as it gets killed.

我需要运行一项服务,该服务将在后台连续(24/7)提供位置(此应用仅适用于特定人群,因此无需担心电池寿命).

I need to run a service which will give location continuously (24/7) in background (This app is only for specific people so no worries about battery life).

我试过了:

  1. 运行前台服务.

  1. running foreground service.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(intent)
    } else {
            startService(intent)
    }

也在服务中 onCreate 方法以通知开始

Also in service onCreate method started with notification

@Override
public void onCreate() {
    Log.i("Service", "onCreate");
    startForeground(NOTIFICATION_ID, getnotification());
}

  • onStartCommand

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        initLocationClient();
        initLocationSyncThread();
    
        return START_STICKY;
    }
    

  • onDestroyonTaskRemoved 中重新启动服务,但它们没有被调用.

  • re-initiating service in onDestroy and onTaskRemoved but they are not getting called.

    绑定服务

    经常调度警报管理器并启动服务,但 Play 商店会警告我们的应用程序使用警报管理器过于频繁,这是一种不好的做法.现在有办法使用workmanager来安排少于15分钟的时间,但仍然不能保证在15分钟后开始.

    scheduling alarm manager and start service frequently but play store will give warning that our app is using alarm manager too frequently and its a bad practice. And there is now way using workmanager to schedule for less than 15 min and its still not guaranteed to start after 15 min.

    那么除了上述选项之外,还有什么方法可以继续运行服务吗?

    So is there any way to keep running a service other than above options?

    推荐答案

    如果您通过 THE LINK,你会发现:

    If you go through THE LINK, you will find:

    不幸的是,有些设备将最近菜单中的应用杀死作为强制停止.Stock Android 不会这样做.当应用被强制停止时,它无法执行作业、接收警报或广播等.很遗憾,我们无法解决它 - 问题出在操作系统上,没有解决方法.

    Unfortunately, some devices implement killing the app from the recents menu as a force stop. Stock Android does not do this. When an app is force stopped, it cannot execute jobs, receive alarms or broadcasts, etc. So unfortunately, it's infeasible for us to address it - the problem lies in the OS and there is no workaround.

    这是一个已知问题.很多厂商为了省电,强制关闭APP,取消了所有的定时任务、闹钟、广播接收器等.主要厂商有OnePlus(你可以选择toogle)、Redmi、Vivo、Oppo、Huwaei.

    It is a known issue. To save battery, many manufacturers force close the app, thus cancelling all the period tasks, alarms, and broadcast receivers etc. Major manufacturers being OnePlus (you have option to toogle), Redmi, Vivo, Oppo, Huwaei.

    这些设备中的每一个都有 AutoStartManagers/AutoLaunch/StartManager 类型的优化管理器.从而防止后台活动再次启动.您必须手动要求用户将您的应用程序列入白名单,以便应用程序可以自动启动其后台进程.按照这个这个 链接,了解更多信息.

    Each of these devices have AutoStartManagers/AutoLaunch/StartManager type of optimization managers. Which prevent the background activities to start again. You will have to manually ask the user to whitelist your application, so that app can auto start its background processess. Follow THIS and THIS link, for more info.

    添加到不同制造商的白名单的方法在这个堆栈溢出答案中给出.即使添加到白名单后,您的应用程序也可能因打盹模式而无法运行,为此您必须忽略电池优化

    The methods to add to whitelist for different manufactures are give in this Stack Overflow answer. Even after adding to whitelist, your app might not work because of DOZE Mode, for that you will have to ignore battery otimizations

    另外,如果您可能想知道,Gmail/Hangout/WhatsApp/Slack/LinkedIn 等应用已经被这些 AutoStart Manager 列入白名单.因此,对其后台进程没有影响.您总是会及时收到更新和通知.

    Also in case you might be wondering, apps like Gmail/Hangout/WhatsApp/Slack/LinkedIn etc. are already whitelisted by these AutoStart Managers. Hence, there is no effect on their background processes. You always receive timely updates & notifications.

    这篇关于有没有办法连续运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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