WakefulBroadcastReceiver

WakefulBroadcastReceiver

本文介绍了BroadcastReceiver的VS WakefulBroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释我在breify,是什么的BroadcastReceiver和WakefulBroadcastReceiver之间准确的区别。

can Somebody explain me in breify, what is the exact difference between BroadcastReceiver and WakefulBroadcastReceiver.

在这情况下,我们一定要到这个接收器。

In which situation we have to go to this receivers.

推荐答案

之间存在的BroadcastReceiver WakefulBroadcastReceiver

当您收到内部的onReceive广播()方法

假设,

的BroadcastReceiver

  • 不保证 CPU会保持清醒如果您启动一些长时间运行的进程。 CPU可立即回到入睡。
  • It is not guaranteed that CPU will stay awake if you initiate some long running process. CPU may go immediately back to sleep.

WakefulBroadcastReceiver

  • 保障 CPU会保持清醒,直到你火 completeWakefulIntent
  • It is guaranteed that CPU will stay awake until you fire completeWakefulIntent.

例如:

在这里,当你接收广播,你开始一个服务,因为你正在使用 WakefulBroadcastReceiver ,将持有 wakelock ,不会让CPU睡眠直到完成内部供水和消防工作的 completeWakefulIntent

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

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);
    }
}

    public 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);
    }
}

这篇关于BroadcastReceiver的VS WakefulBroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 14:46