0

I m actually developping an application in which I need my phone to be active even if it's locked.

Explanations

The applications connect to a remote nodejs server using socket.io. In that case, it cans sends realtime messages to a socket server that can handle this and make anything it needs. The fact is the socket management is in a service class (extends Service class).

The problem is that, when I lock the phone, the device stop to send heartbeat, and so is disconnected (by timeout) from the nodejs socket io server.

Actually, the normal behaviour of an android phone locked is to sleep. Meaning no activity.

Question

I was wondering how does others applications to be able to receive notifications, and so handling notif by a background service. it means that there's an activity even if the phone is sleeping right ?

How can I do to make this without draining a lot of battery ?

mfrachet
  • 8,772
  • 17
  • 55
  • 110

2 Answers2

0

You use wake locks to keep CPU awake while locked: https://developer.android.com/training/scheduling/wakelock.html

You should aquire it for a small amount of time to not drain battery.

marcinj
  • 48,511
  • 9
  • 79
  • 100
-1

I think if you will acquire a phone wake lock it will drain the battery which is not a good thing . On the other hand, The use of service is really very good,and you talk about the notification of other apps and they works in background because notification does this by default

So in you case As I do not know what are you really trying to do with server, But Service is good option. Service works even your mobile is locked. But in your case if it stops make sure it is not bind with the class.

You should make sure that the Service is not binded with the class or activity as when the activity will be destroyed the service would also be. Just trigger your service and let it handle all the things in background.Please read the discussion in this link. It might help you in understanding better.

Community
  • 1
  • 1
Coas Mckey
  • 701
  • 1
  • 13
  • 39