在后台运行的BroadcastReceiver,当设备alseep? [英] Run BroadcastReceiver in the background and when device is alseep?

查看:122
本文介绍了在后台运行的BroadcastReceiver,当设备alseep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的广播接收器设置当用户得到一个接收到的SMS做一些事情。但我需要它在后台运行,当设备处于睡眠状态。所以,我会用一个服务启动该广播接收器?如果是这样,有人可以给我一些假code?而如何将设备是否睡着了?这项工作


解决方案

  

我有一个简单的BroadcastReceiver设置当用户得到一个接收到的SMS做一些事情。


确定。


  

但我需要它在后台运行,当设备是睡着了。


不是真的。


  

所以我会用一个启动的BroadcastReceiver服务?


没有。你的广播接收器应在清单中,因此它可以不管你的任何code的其余部分正在运行的调用。这就是为什么我说:不是真的到后台运行 - 你不想code运行所有的时间背景。相反,你希望能够在任何时候接收广播,这就是把接收器清单是


  

如果是这样,有人可以给我一些假code?


<一个href=\"https://github.com/commonsguy/cw-advandroid/tree/master/SMS/Monitor\">https://github.com/commonsguy/cw-advandroid/tree/master/SMS/Monitor


  

和如何想,如果该设备是睡着了?这项工作


不会。然而,进入的短信,像一个呼入电话,将唤醒设备。

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?

解决方案

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.

这篇关于在后台运行的BroadcastReceiver,当设备alseep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆