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?

- 53,877
- 76
- 193
- 251
1 Answers
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.

- 986,068
- 189
- 2,389
- 2,491