How to wake up an Android on incoming data

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I want an Android service, which is listening for data (I have tried TCP and UDP) to wake up the service/phone when data arrives.

Currently the phone stays asleep when IP data arrives and that data is lost. Is this normal for Android? I see sleeps of 2+ hours several times a day, and 7+ hour sleeps when I am asleep.

Does GCM provided a solution which wakes up a service/phone when a message arrives.

I need messages to be displayed within a few seconds of them being sent from the server.

Can this only be achieved by keeping the CPU running all day long? Is there no interrupt from the GSM modem on the arrival of data?

回答1:

As I understand it, if you have an outstanding socket connection over mobile data to a server, that socket connection is maintained while in sleep mode, and incoming packets will wake up the phone out of sleep mode (briefly) to deliver the data.

The same is not true of WiFi (and presumably not for hardwired Ethernet, either). You would need to hold a WakeLock and a WifiLock.

Does GCM provided a solution which wakes up a service/phone when a message arrives.

Yes, but...

I need messages to be displayed within a few seconds of them being sent from the server.

First, GCM has no service-level guarantee. I am not aware of any solution that does, including anything that you might roll yourself (e.g., MQTT).

Second, you have no idea if the device is capable of receiving any such messages (GCM or otherwise), as the device may be turned off, in airplane mode, etc.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!