안드로이드 foreground 서비스로 notify를 하면서 화면이 잠긴상태로 웹 통신을 하고 싶어요
웹 서버는 잘 구현되어있는 상태이구요, 화면이 켜진 상태로는 통신이 잘 되는데
화면이 잠금상태로 돌아가면 몇분 지나 웹 통신이 끊어져 버립니다
블루투스는 끊어지지 않는데 말이죠
웹 연결이 잠금 상태(doze mode)에서도 끊어지지 않는 서비스를 구현할 수 있는 방법이나
예시코드를 제공해주시면 감사하겠습니다
아래는 제가 시도한 코드입니다 (웹 부분만)
@Override
public int onStartCommand(Intent intent, int flags, int startId){
try {
webSocket = IO.socket(url);
}catch(URISyntaxException e){
e.printStackTrace();
}
webSocket.connect();
webSocket.on("message",onListen); //'메세지'이벤트에 대한 리스너 콜백
webSocket.emit("join",role);
bluetooth_init(device); //블루투스 스레드 실행
return START_REDELIVER_INTENT;
}
//웹 이벤트에 대한 리스너
private Emitter.Listener onListen = new Emitter.Listener() {
@Override
public void call(Object... args) {
//서버 메세지에 대한 핸들링이다
JSONObject data = (JSONObject) args[0];
try {
Log.d("gd", "listener listened");
String receivedMessage = data.getString("messagecontent");
Log.d("gd", "received from server:");
Log.d("gd", "message: " + receivedMessage);
}
catch (JSONException e) {
e.printStackTrace();
}
}//CALL
};
화면이 켜져있는 상태에서는 리스너 및 emit이 되는데, 꺼져있는 상태에서 금방 서버에서 이탈을 해버려요.