Java BroadcastReceiver 与 WakefulBroadcastReceiver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26380534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BroadcastReceiver Vs WakefulBroadcastReceiver
提问by user2107111
Can somebody explain what the exact difference is between BroadcastReceiver
and WakefulBroadcastReceiver
?
有人能解释一下BroadcastReceiver
和之间的确切区别WakefulBroadcastReceiver
吗?
In what situations would we have to use each Receiver class?
在什么情况下我们必须使用每个 Receiver 类?
采纳答案by Mehul Joisar
There is only one difference between BroadcastReceiver
and WakefulBroadcastReceiver
.
BroadcastReceiver
和之间只有一个区别WakefulBroadcastReceiver
。
When you receive the broadcast inside onReceive()
method,
当你收到广播里面的onReceive()
方法时,
Suppose,
认为,
BroadcastReceiver:
广播接收器:
- It is not guaranteedthat CPU will stay awakeif you initiate some long running process. CPU may go immediately back to sleep.
- 它不保证该CPU将保持清醒,如果你启动一些长时间运行的进程。CPU 可能会立即返回休眠状态。
WakefulBroadcastReceiver:
唤醒广播接收器:
- It is guaranteedthat CPU will stay awakeuntil you fire
completeWakefulIntent
.
- 这是保证该CPU将保持清醒,直到你开除
completeWakefulIntent
。
Example:
例子:
Here, when you receive broadcast, you are starting a service, as you are using WakefulBroadcastReceiver
, it will hold wakelock
and won't let the CPU sleep until you finish the work inside service and fire completeWakefulIntent
在这里,当您收到广播时,您正在启动一个服务,正如您正在使用的那样WakefulBroadcastReceiver
,它会保持wakelock
并且不会让 CPU 休眠,直到您完成服务内部的工作并触发completeWakefulIntent
Code:
代码:
public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This is the Intent to deliver to our service.
Intent service = new Intent(context, SimpleWakefulService.class);
// Start the service, keeping the device awake while it is launching.
Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
startWakefulService(context, service);
}
}
class SimpleWakefulService extends IntentService {
public SimpleWakefulService() {
super("SimpleWakefulService");
}
@Override
protected void onHandleIntent(Intent intent) {
// At this point SimpleWakefulReceiver is still holding a wake lock
// for us. We can do whatever we need to here and then tell it that
// it can release the wakelock. This sample just does some slow work,
// but more complicated implementations could take their own wake
// lock here before releasing the receiver's.
//
// Note that when using this approach you should be aware that if your
// service gets killed and restarted while in the middle of such work
// (so the Intent gets re-delivered to perform the work again), it will
// at that point no longer be holding a wake lock since we are depending
// on SimpleWakefulReceiver to that for us. If this is a concern, you can
// acquire a separate wake lock here.
for (int i=0; i<5; i++) {
Log.i("SimpleWakefulReceiver", "Running service " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
SimpleWakefulReceiver.completeWakefulIntent(intent);
}
}