有什么办法可以连续运行服务吗? [英] Is there any way to run service continuously?

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

问题描述

在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 store会警告我们的应用程序过于频繁地使用警报管理器,这是一种不良做法.现在,有一种方法可以使用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.

    这是一个已知问题.为了节省电池,许多制造商强行关闭了该应用程序,从而取消了所有定期任务,警报和广播接收器等.主要制造商为OnePlus(您可以选择切换),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.

    要添加到不同制造商白名单的方法,请参见此堆栈溢出答案.即使添加到白名单之后,由于打might"模式,您的应用也可能无法运行,因为您必须以编程方式

    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等应用列入了白名单.因此,对其背景过程没有影响.您总是会及时收到更新信息.通知.

    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天全站免登陆