我设置了一个简单的BroadcastReceiver
,以便在用户收到传入短信时执行某些操作。但我需要它在后台运行并且当设备处于 sleep 状态时运行。那么我会使用启动 BroadcastReceiver
的 Service
吗?如果是这样,有人可以给我一些伪代码吗?如果设备处于 sleep 状态,这将如何工作?
最佳答案
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?
没有。您的 BroadcastReceiver
应该位于 list 中,因此无论您的其余代码是否正在运行,都可以调用它。这就是为什么我说“实际上不是”“在后台运行”——你不希望代码一直在后台运行。相反,您希望能够在任何时候接收广播,这就是将接收器放入 list 中的目的。
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?
不会的。但是,传入的短信(如来电)会唤醒设备。
关于java - 当设备处于 sleep 状态时在后台运行广播接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4979790/