如何在Android O中使服务在后台运行? [英] How to keep service running in background in Android O?

查看:202
本文介绍了如何在Android O中使服务在后台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保持服务在后台运行.该服务用于保持Socket.IO/MQTT连接.在Android O之前的版本中,保持服务正常运行没有问题,但是在Android O中这是不可能的,因为后台执行限制.

I need to keep a service running in background. This service is used to keep a Socket.IO/MQTT connection. In versions previous to Android O there were no problem keeping the service alive, but in Android O this is not possible because of the background execution limits.

除了前台服务之外,还有什么方法可以保持服务运行?

Is there any way to keep the services running, besides foreground service?

推荐答案

您应该使用Firebase Cloud Messaging从服务器推送数据,这是为了防止所有应用打开自己的连接并保持活动状态,耗尽电池电量并填满RAM对用户没有任何价值.您的应用也不例外.

You should use Firebase Cloud Messaging to push data from a server, this is to prevent all apps opening their own connection and staying active, draining the battery and filling up the RAM for no value to the user. Your app should be no exception to that.

对于短暂的连接(分钟)或用户会话(分钟,除非用户保持更长的活动时间),前台服务可能是解决方案.

For short lived connections (minutes), or user sessions (minutes, too, unless the user stays active longer), a foreground service might be a solution.

出于好奇,您想要持久连接"的用例是什么?

Out of curiosity, what is your use case for wanting a "persistent connection"?

这篇关于如何在Android O中使服务在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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