I have an Android app in which the chat client is one of its features. The chat client uses Smack- based XMPP for Android and runs Openfire as an XMPP server in the background. The connection is established using BOSH. All XMPP connection processing is implemented as a service for starting and listening in the background for incoming messages, even if there is no application activity in the foreground. So far, everything is working fine.
The only problem is sleep mode. In the emulator (when set to "Stay Awake") or using a phone, XMPP connections are held and the application can send and receive messages. However, as soon as the phone goes into sleep mode, the XMPP connection breaks - I see it in the Openfire server admin console that the user is offline. Intuitively, I want to receive messages all the time, for example, WhatsApp.
Of course, I searched on the Internet, including Stackoverflow, but I could not get the final answer. Often a precedent is that the task should be performed periodically, say, once an hour. But this is not like fir in the case of a chat client. Since I assume this is a common use case - after all, there are so many chat apps or apps with chat features, this is my question:
How can I change / expand the application so that I can receive a chat message during sleep?
I came across WakeLock . Is this a way to go or are they not suitable for my use?
With Lollipop, there is also the JobScheduler API, which itself uses WakeLock . It is better?
How, for example, WhatsApp handles this case?
On the one hand: I have problems with the standby mode, using the emulator for debugging. When I turn off "Stay Awake" in the emulator, the screen turns black after 1+ minutes and the XMPP connection is disconnected. But somehow I have no idea how to wake up / switch the emulator back as soon as it gets dark. Android Studio actually tells me at some point that the device or something is gone, and I have to restart the emulator again.
source share