如何在android 7.0及更高版本中保持活动套接字连接? [英] How to keep alive socket connection in android 7.0 and above?

查看:66
本文介绍了如何在android 7.0及更高版本中保持活动套接字连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Socket.io 库开发多人游戏.它运作良好.但是,在android 7.0及更高版本中,当我的应用程序处于后台时,系统会自动暂停所有网络工作.(而且我必须保持我的套接字连接有效).我按照此处所述进行了研究.但是,我听不懂.因此,请为此提供解决方案.

解决方案

不幸的是,这方面有个坏消息和一些好消息.

坏:

自从Android棉花糖及以上版本开始,就有了打ze睡模式的概念.如果设备保持放置状态一段时间(无法确认其持续时间,并且未由Google披露),则设备将进入打ze模式并暂停所有网络活动.将有简短的维护窗口,您可以在其中进行同步和填充.较小的解决方法,不要针对23种以上的api,我之所以说很小是因为我观察到这在某些手机上不起作用.另一种可能绕过此方法的方法是将您的应用从电池限制列入白名单,但是根据Google准则,我认为您的应用不符合要求.

更糟糕的消息是,从API 26开始,当应用程序完全处于后台且没有可见的组件(通知或前台服务等)时,后台服务也将被完全挂起.所以奥利奥会更糟.

好:

您可能真的不想一直保持套接字打开状态.而是选择同步突发.我个人每隔30-60分钟运行一次作业,以尝试进行同步.

您可以利用JobScheduler api,它将自动处理打ze模式和内容,并且可以使它们在有Internet连接时定期运行.在作业运行时,您可以连接到服务器,执行操作并关闭套接字.这就是Google想要的,并且正在推动所有开发人员朝着这个方向前进.

另一种选择是让前台服务显示激活的通知,并通过您的套接字不断地将其连接.这对用户将是可见的,不仅使他们烦恼您一直在使用他们的数据,而且还可能对电池不利.另一个替代方法是使用作业调度程序定期调度和运行前台服务,以使其透明,同时还不时地同步数据.后一种方法是WhatsApp所做的,它们有一个正在运行的作业,该作业会不时地将所有传入消息与前台服务同步.

简而言之:

您将无法使其始终处于活动状态.您可以尝试使用我目前描述和了解的一种方法来突发尝试(也许还有其他我不知道的替代方法,我已经测试了这些方法并且它们起作用了),抱歉,您将不得不妥协.

I am developing multiplayer game using Socket.io library. it Works Well. But, in android 7.0 and above, system automatically suspend all network work when my app is in background. (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. 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. 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...). 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.

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

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.

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.

这篇关于如何在android 7.0及更高版本中保持活动套接字连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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