当设备 [Galaxy s3 19300, Android 4.1.2] 处于 WAKE 状态时,我需要使用处于后台服务中的线程中的 ServerSocket 接受数据。但是当设备处于睡眠模式(即屏幕关闭时)时,ServerSocket 似乎不接受。
我尝试使用 WIFI 锁,全部和部分以及 POWER 锁,但是这两个选项都无法让 ServerSocket 监听。
有什么线索可以解决这个问题吗?
编码
// -------------------------------------------------------------------------
private void CallSock() {
Log.i("$$$$$",
"Before WiFI State is " + Integer.toString(wm.getWifiState()));
wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock");
PowerManager mgr = (PowerManager) mContext
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakeLock");
wakeLock.acquire();
if (!wifiLock.isHeld()) {
wifiLock.acquire();
Log.i("$$$$$", "Wifi lock acquired");
}
Looper.prepare();
sSocket = new ServerSocket(serverPort);
while (true) {
Log.i("$$$$$", "In the While loop WiFI State is "
+ Integer.toString(wm.getWifiState()));
Rulecontent.writeLogInfo("Waiting to recieve file");
String fileName = readFile();
}
}
// -------------------------------------------------------------
public String readFile() {
StringBuilder x = null;
try {
InputStream in = null;
Socket recvClientSocket = sSocket.accept();
in = recvClientSocket.getInputStream();
byte[] bytes = new byte[1024];
x = new StringBuilder();
int numRead = 0;
while ((numRead = in.read(bytes)) >= 0) {
x.append(new String(bytes, 0, numRead));
}
} catch (IOException e) {
e.printStackTrace();
}
return x.toString();
}
// -------------------------------------------------------------------------