在Android Marshmallow中接收UDP [英] Receive UDP in Android Marshmallow

查看:77
本文介绍了在Android Marshmallow中接收UDP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的Nexus 5X(棉花糖)从服务器不断接收UDP数据包时遇到问题

我还有另一个可以接收所有UDP数据包的真实设备,但是Android API 23似乎有所改变.

代码:

有人遇到同样的问题吗?

                        if (s == null || s.isClosed()){


                            Log.v("udp", "----------------------------------------------------new socket---------------------------------");


                            s = new DatagramSocket(null);
                            s.setReuseAddress(true);
                            s.setBroadcast(true);
                            s.setSoTimeout(5000);
                            s.bind(new InetSocketAddress(8002));
                            p = new DatagramPacket(message, message.length);

                            try{
                                Thread.sleep(100);
                            }
                            catch(Exception e){

                            }
                        }



                        //Log.v("test","---------------------------------------------------- Pas1 ---------------------------------");
                        message = new byte[100];
                        //p = new DatagramPacket(message, message.length);
                        p.setLength(message.length);

                        s.receive(p);

问题是:

我随机接收广播数据包,而在其他真实设备中,我接收到所有数据包.

我确定android api 23中的某些内容已更改.看来,如果接收函数仅在缓冲区中有数据包,则触发"它,但是如果在没有它内部"的情况下调用它,则会出现sockettimeout./p>

谢谢!

解决方案

Android Marsmallow引入了打ze模式,该模式将在一段时间后暂停网络访问.也许这也会影响您的应用程序.

"在Doze中,以下限制适用于您的应用:

-网络访问已暂停

-... "

http://developer.android.com/training/Monitoring-device-state/doze-standby.html

坏消息是您不能做很多事情,除了要求用户将您的应用程序置于免电池优化的应用程序之列.不过请务必小心,因为Google可能会为此将您的应用程序从Play商店中删除(这就是为什么我没有提供其源代码的原因).

"但是,Google认为什么是应用程序的核心功能"才是最重要的.不是用户,不是开发人员.Google.用户和开发人员可能会在添加内容的过程中保持完美的和谐.进入白名单是指该应用程序,但如果Google认为这不是必需的,则Google会将自己设置为比用户更重要的应用程序,并禁止该应用程序.(

The problem is:

I receive randomly the broadcast packets, meanwhile in other real devices I receive all.

I'm sure that something was changed in android api 23. It seems that the receive function only "triggers" if it has a packet in the buffer, but if it's called without it "inside", the sockettimeout appears.

Thanks!

解决方案

Android Marsmallow introduces doze mode, which will suspend network access after some time. Maybe this affects your application as well.

"The following restrictions apply to your apps while in Doze:

- Network access is suspended

- ..."

http://developer.android.com/training/monitoring-device-state/doze-standby.html

The bad news is that you cannot do much about it, except for asking the user to put your application on the list with applications exempted from battery optimization. Be careful with this though, because Google will likely remove your application from the Play store for it (this is why I didn't include source code for it).

"However, it is Google’s opinion of what "the core function of the app" is that matters. Not the user. Not the developer. Google. The user and developer might be in perfect harmony over what adding the app to the whitelist means, but if Google thinks that it is not warranted, Google sets themselves up as being more important than the user and bans the app." (https://commonsware.com/blog/2015/11/11/google-anti-trust-issues.html)

Mostly the only viable option is to use Google Cloud messaging, but that might not be a good solution in your case.

这篇关于在Android Marshmallow中接收UDP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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