我当前正在构建一个通过WebSocket (via Java WebSockets from TooTallNate)连接到服务器的Android应用。问题是在我的Moto Z Play(Android 8.0)和Acer Iconia A3-A40(Android 6.0)上,WebSocket在随机时间(介于8到25分钟之间)后关闭。在我的HTC ONE(Android 5.0)上,在我添加了唤醒锁和wifilock(让它运行3h,运行完美)之后,这没有发生。在Android Studio模拟器上不会发生此问题。 在我的Moto Z Play上,在关闭WebSocket之前,我收到以下日志:
> 12-14 19:16:23.409 1443-2088/? I/chatty: uid=1000(system) IpManager.wlan0 expire 1 line
12-14 19:16:23.411 946-1266/? D/CommandListener: Setting iface cfg
12-14 19:16:23.412 1443-16705/? I/chatty: uid=1000 system_server expire 1 line
12-14 19:16:23.412 1443-16703/? I/chatty: uid=1000 system_server expire 1 line
12-14 19:16:23.461 2125-2125/? E/wpa_supplicant: disconnect rssi= -68
12-14 19:16:23.462 2125-2125/? I/wpa_supplicant: wlan0: CTRL-EVENT-DISCONNECTED bssid=f5:eb:3d:bf:de:b6 reason=3 locally_generated=1 disconnect_rssi=68
12-14 19:16:23.464 2125-2125/? E/wpa_supplicant: nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory
nl80211: Failed to set IPv4 unicast in multicast filter
12-14 19:16:23.474 946-1257/? I/Netd: Destroyed 1 sockets on <the ipadress of the smartphone> in 1.0 ms
12-14 19:16:23.479 2125-2125/? E/wpa_supplicant: eap_proxy: eap_proxy_notify_config
nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory
12-14 19:16:23.479 20544-20708 D/SocketSerivce: onClose (Here the WebSocket is closed)
12-14 19:16:23.479 2125-2125/? E/wpa_supplicant: nl80211: Failed to set IPv4 unicast in multicast filter
12-14 19:16:23.493 2125-2125/? E/wpa_supplicant: eap_proxy: eap_proxy_notify_config
eap_proxy: eap_proxy_notify_config
我做了一些研究,认为这将是一个打do睡的问题,它会暂停网络访问,但是有一些反对意见:
(但我可能是错的)
我到目前为止所做的事情:
通过以下方式启动ForegroundService:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
}else{
startService(intent);
}
获取wakeLock和wifilock(并在websocket关闭时释放):
powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WakeLockMyApp");
wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "WifiLockMyApp");
wakeLock.acquire();
wifiLock.acquire();
检查了onStartCommand-Method
startForeground(..)
return START_STICKY;
提前感谢