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

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

问题描述

我在使用新的 Nexus 5X (Marshmallow) 不断接收来自服务器的 UDP 数据包时遇到问题

I'm having problems to receive constantly the UDP packets from the server with new Nexus 5X (Marshmallow)

我有另一个接收所有 UDP 数据包的真实设备,但似乎在 Android API 23 中发生了一些变化.

I have another real devices that receive all UDP packets, but it seems that something changed in Android API 23.

代码:

有人遇到同样的问题吗?

Anyone with the same problem?

                        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);

问题是:

我随机接收广播包,同时在其他真实设备中我全部接收.

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

我确定在 android api 23 中发生了一些变化.似乎接收函数只有在缓冲区中有数据包时才会触发",但如果在内部"没有数据包的情况下调用它,则会出现 sockettimeout.

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.

谢谢!

推荐答案

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

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

"以下限制适用于您在打瞌睡时的应用:

- 网络访问被暂停

- ..."

http://developer.android.com/training/监控设备状态/doze-standby.html

坏消息是,除了要求用户将您的应用程序与免于电池优化的应用程序一起列入列表之外,您对此无能为力.不过请注意这一点,因为 Google 可能会为此从 Play 商店中删除您的应用程序(这就是我没有包含它的源代码的原因).

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

"然而,重要的是谷歌对应用程序的核心功能"的看法.不是用户.不是开发者.谷歌.用户和开发者可能会在添加将应用程序添加到白名单意味着,但如果 Google 认为没有必要,Google 会将自己设置为比用户更重要并禁止该应用程序." (https://commonsware.com/blog/2015/11/11/google-anti-trust-issues.html)

大多数情况下,唯一可行的选择是使用 Google Cloud 消息传递,但在您的情况下这可能不是一个好的解决方案.

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