Run BroadcastReceiver in the background and when device is alseep?

蹲街弑〆低调 提交于 2020-01-07 06:42:52

问题


I have a simple BroadcastReceiver set up to do something when the user gets an incoming SMS. But I need it to run in the background and when the device is asleep. So would I use a Service that starts the BroadcastReceiver? If so, can someone give me some pseudo-code? And how would this work if the device is asleep?


回答1:


I have a simple BroadcastReceiver set up to do something when the user gets an incoming SMS.

OK.

But I need it to run in the background and when the device is asleep.

Not really.

So would I use a Service that starts the BroadcastReceiver?

No. Your BroadcastReceiver should be in the manifest, so it can be invoked regardless of whether any of the rest of your code is running. That's why I say "not really" to "run in the background" -- you DO NOT WANT code running all the time in the background. Rather, you want to be able to receive broadcasts at any point, and that is what putting the receiver in the manifest is for.

If so, can someone give me some pseudo-code?

https://github.com/commonsguy/cw-advandroid/tree/master/SMS/Monitor

And how would this work if the device is asleep?

It won't. However, an incoming SMS, like an incoming phone call, will wake up the device.



来源:https://stackoverflow.com/questions/4979790/run-broadcastreceiver-in-the-background-and-when-device-is-alseep

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