有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当设备未连接到充电器时,java NetD会破坏插座

我有一个非常恼人的问题,我不明白发生了什么:

我们正在编写一个库,通过蓝牙建立一个socket。之后,我们在localhost上注册一个服务器。现在有几个应用程序正在建立与本地主机的连接并发送/接收数据。服务器正在绑定连接并通过蓝牙处理通信

自从安卓7.0以来,我们就遇到了这个问题NetD经常破坏本地主机的socket。我们可以在Logcat中看到以下输出:

>     06-23 12:38:17.576   522  2676 I Netd    : Destroyed 3 sockets for UidRanges{ 10000-2147483647 } skip={1001 1027 10010 10013 10018 10021
> 10035 10037 10064 10081} in 1.2 ms

所有设备都运行库存ROM,我对其进行了以下测试:

  • 三星S7
  • 三星S8
  • Nexus 5x
  • 三星S6
  • 谷歌像素

我很确定,这是一种节省电池的机制,因为在给设备充电时不会发生这种情况

我希望任何人都能帮助我理解这里发生的事情


共 (1) 个答案

  1. # 1 楼答案

    我相信这个问题来自Android 6.0 API级别23中引入的新Doze功能

    Doze improves battery life by deferring CPU and network activities when a user leaves a device unplugged, stationary, and with the screen turned off. Android 7.0 brings further enhancements to Doze by applying a subset of CPU and network restrictions while the device is unplugged with the screen turned off, but not necessarily stationary, for example, when a handset is traveling in a user’s pocket.

    打盹限制

    打瞌睡时,以下限制适用于你的应用程序:

    • 网络访问被暂停
    • 系统会忽略尾流锁
    • 标准AlarmManager报警(包括setExact()和setWindow())将推迟到下一个维护窗口。
      • 如果需要设置打盹时触发的警报,请使用setAndAllowHileIDLE()或setExactAndAllowHileIDLE()
      • 使用setAlarmClock()设置的报警继续正常触发-系统在这些报警触发前不久会停止打盹
    • 系统不执行Wi-Fi扫描
    • 系统不允许运行同步适配器
    • 系统不允许JobScheduler运行

    话虽如此,从技术上讲,你有两个选择——拥抱打瞌睡,与之抗争

    拥抱睡意

    过一遍Optimizing for Doze and App Standby

    打瞌睡

    dumpsys deviceidle disableidle_to设置为1秒(相关比例因子设置为1)的组合将有助于禁用它。请参阅此xda-developers post