关于WakefulBroadcastReceiver

时间:2015-09-07 11:20:07

标签: android broadcastreceiver android-wake-lock

我从这个链接一直在研究WakefulBroadcastReceiver:https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

我对此几乎没有什么困惑:

  1. 即使设备处于睡眠模式,此接收器是否确保您将接收广播? (我认为不,它只是在收到广播后让设备保持清醒,直到调用completeWakefulIntent()为止。)
  2. 文档说明了在接收者中使用意图服务,并在完成工作后,调用了completeWakefulIntent。
  3. 代码:

    import android.app.IntentService;
    import android.content.Intent;
    import android.os.SystemClock;
    import android.util.Log;
    
    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);
        }
    }
    

    现在,正如文档说这个接收器有一个唤醒锁定,我非常怀疑从接收器开始像意图服务这样的事情是一个好习惯。这会阻止接收器释放唤醒锁并耗尽大量电池,因为服务通常用于长时间操作

    即使上面的代码片段突出显示,在释放锁定之前还有25秒的延迟。这是使用此接收器的正确方法还是仅用于短时间操作?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:10)

  

即使设备处于睡眠模式,此接收器是否确保您接收广播?

没有

  

(我认为不,它只是在收到广播后让设备保持唤醒状态,直到调用completeWakefulIntent()为止。)

正确。

  

我非常怀疑从接收器启动类似意图服务的好方法

是的,这是一个很好的做法。这就是重点。

  

这会阻止接收器释放唤醒锁

唤醒锁定保存在static数据成员中,这就是static方法(completeWakefulWork())能够释放它的原因。

  

因为服务通常用于长期运营

&#34; long&#34;的定义变化。我开始使用WakefulBroadcastReceiver(或我的WakefulIntentService前任)来处理可能超过10秒的任何事情。但是,任何形式的IntentService实际上只是为交易工作而设计的(例如,下载大文件),因此它不适合服务可能需要无限期运行的情况(例如,因为用户想要与某个聊天服务器通话。)

如果要确保CPU保持活动状态以使服务完成其工作,则仅使用WakefulBroadcastReceiver。否则,您直接使用IntentService

  

这是使用此接收器的正确方法还是仅用于短时间操作?

25秒是使用WakefulBroadcastReceiver及其关联的IntentService完全合理的时间范围。

  

但更复杂的实现可能会在释放接收器之前将其自身唤醒锁定

当然,虽然不清楚这会给你带来什么。唤醒锁是一种唤醒锁。服务器拥有的唤醒锁定对电池的影响与接收器拥有的唤醒锁定完全相同。