0

Im creating a live chat messenger just like LINE, Im using socket.io, but when my device goes to sleep mode, the connection get lost. Right now I solve this problem using PARTIAL_WAKE_LOCK but I read that it affect the battery life. There is another way to solve this problem?

pd I also try to use onStartCommand START_STICKY but this neither work.

thanks in advance!!

this is part of my code

@Override
public void onCreate(){
        handler = new Handler();
        mSocket.connect();
        localStorage = new LocalStorage(this);
        String room = localStorage.getRoom();
        mSocket.emit("join room", room);
        mSocket.on("chat message", onNewMessage);
        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MessageService");
        wl.acquire();
}

@Override
public void onDestroy(){
    wl.release();
    super.onDestroy();
    mSocket.off("chat message", onNewMessage);
    mSocket.disconnect();
}
  • Once refer the android push notifications tutorials..http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ – Chandrakanth Apr 02 '15 at 06:04
  • thanks for your answer. actually I forgot to say that Im not interested in GCM, thats why Im using socket.io. but really good explanation about GCM its helps me a lot to understood it. – Nicolas Gomez Apr 02 '15 at 07:08
  • Is this the code of your service class you posted? You need to override `onStartCommand` method with return statement STAR_STICKY or START_NOT_STICKY to keep it running in background. – Apurva Apr 03 '15 at 07:38
  • hello thanks for the answer but I already tried also onStartCommand and the result is the same, the connection get lost. – Nicolas Gomez Apr 03 '15 at 17:18
  • that can help [Keep a Service running even when phone is asleep?](http://stackoverflow.com/questions/8713361/keep-a-service-running-even-when-phone-is-asleep) – Moustafa Bayommy Oct 20 '15 at 16:27

0 Answers0