简体   繁体   English

如何在 android 7.0 及更高版本中保持活动套接字连接?

[英]How to keep alive socket connection in android 7.0 and above?

I am developing multiplayer game using Socket.io library.我正在使用Socket.io库开发多人游戏。 it Works Well.它运作良好。 But, in android 7.0 and above, system automatically suspend all network work when my app is in background.但是,在 android 7.0 及更高版本中,当我的应用程序处于后台时,系统会自动暂停所有网络工作。 (And I must need to keep alive my socket connection) . (而且我必须保持我的套接字连接) I research about it as described here .我按照此处的描述对其进行了研究。 but, i can't understand.但是,我无法理解。 So, Please provide solution for that.所以,请为此提供解决方案。

Unfortunately there's bad news and some good news for you on this.不幸的是,这方面有一些坏消息和一些好消息。

Bad:坏的:

Since android marshmallow and above, there's a concept of a doze mode.自 android marshmallow 及更高版本以来,就有了打瞌睡模式的概念。 If the device stays put for some time (can't confirm the duration for this and not disclosed by google), the device will go into doze mode and will suspend all network activity.如果设备放置一段时间(无法确认持续时间且谷歌未披露),设备将进入休眠模式并暂停所有网络活动。 There will be short maintenance windows where in you will be able to do syncs and stuff.会有很短的维护窗口,您可以在其中进行同步和其他操作。 Small workaround, do not target 23+ apis, i say small because i have observed this to not work on some phones.小解决方法,不要针对 23+ api,我说小是因为我观察到这在某些手机上不起作用。 Another way to potentially bypass this would be to whitelist your app from battery restrictions but according to google guidelines, i don't think your app will qualify for that.另一种可能绕过此方法的方法是将您的应用列入电池限制的白名单,但根据谷歌指南,我认为您的应用不符合该条件。

Worse news is that start from API 26, background services will also get suspended completely when app is totally backgrounded and has no visible component (a notification or a foreground service etc...).更糟糕的消息是,从 API 26 开始,当应用程序完全在后台并且没有可见组件(通知或前台服务等)时,后台服务也将完全暂停。 So oreo will be worse.所以奥利奥会更糟。

Good:好的:

You might not really want to constantly keep the socket open.您可能真的不想一直保持套接字打开。 Instead opt for bursts of syncs.而是选择突发同步。 I personally have a job run every 30 - 60 mins or so to try and sync up.我个人每 30 - 60 分钟左右运行一次作业以尝试同步。

You can leverage the JobScheduler apis and it will automatically handle the doze modes and stuff and you can make them run periodically when there is internet connection.您可以利用 JobScheduler api,它会自动处理打瞌睡模式和其他内容,您可以让它们在有互联网连接时定期运行。 While the job is running, you can connect to your server, do your thing and shut the socket.在作业运行时,您可以连接到您的服务器,执行您的操作并关闭套接字。 This is what google wants and is pushing all devs towards.这就是谷歌想要的,并且正在推动所有开发者朝着这个方向努力。

UPDATE 19-Apr-2021 WorkManager is the new and the best way to deal with doze mode and background limit restrictions. 2021 年 4 月 19 日更新WorkManager 是处理打瞌睡模式和后台限制限制的最佳新方法。

Another alternative would be to have a foreground service with an active notification displayed which would constantly be connected via your socket.另一种选择是让前台服务显示活动通知,该通知将通过您的套接字不断连接。 This will be visible to the user and it will not only annoy them that you are constantly using their data, it can also be bad for the battery.这对用户来说是可见的,这不仅会惹恼他们,因为您一直在使用他们的数据,而且还会对电池造成伤害。 Alternative to this again is using the job scheduler to schedule and run a foreground service periodically so as to be transparent while also syncing your data every once in a while.另一种方法是使用作业调度程序定期调度和运行前台服务,以保持透明,同时每隔一段时间同步一次数据。 The latter approach is what WhatsApp does, they have a job running which syncs all incoming messages with a foreground service once in a while.后一种方法是 WhatsApp 所做的,他们有一个工作正在运行,偶尔将所有传入消息与前台服务同步。

In Short:简而言之:

You will not be able to keep it alive always.你将无法让它一直保持活力。 You can try doing it in bursts using one of the methods that i described and know currently (maybe there are other alternatives that i don't know, i have tested these and they work) You will have to compromise, sorry.您可以尝试使用我目前描述和知道的一种方法来突发性地执行此操作(也许还有其他我不知道的替代方法,我已经测试过这些并且它们有效)您将不得不妥协,抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM