当手机睡着的BroadcastReceiver行为 [英] BroadcastReceiver behavior when phone is asleep

查看:87
本文介绍了当手机睡着的BroadcastReceiver行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定哪种的行为的BroadcastReceiver ,通过 PackageManager 登记清单中并启用,就是当手机睡着了。这个问题的出现是因为我需要注册的广播接收器,从 WifiManager

 <接收器
    机器人:名称=。receivers.ScanResultsReceiver
    机器人:启用=假>
    <意向滤光器>
        <作用机器人:名称=android.net.wifi.SCAN_RESULTS/>
        <作用机器人:名称=android.net.wifi.WIFI_STATE_CHANGED/>
    &所述; /意图滤光器>
< /接收器>
 

但我想知道(如链接到文档或一些权威帖子在谷歌组)是广播的的保证的醒过来的时候,手机已经睡着一个接收器(如独自离开了一段时间)的并保持手机清醒只要的onReceive()运行的(当然,这不应该是太长,以避免ANR)。
接收器很可能是应用程序运行
的唯一组件 作为奖励,我最近了解到,一些意图,用标记 FLAG_RECEIVER_REGISTERED_ONLY ,是的只交付以动态注册接收器 - ?有没有上市的意图任何地方

解决方案
  

我不是很肯定什么一个BroadcastReceiver的行为,通过PackageManager注册的清单和启用,是当手机处于睡眠状态。

大多数广播不唤醒设备。

  

这节目是保证醒来的时候,手机已经睡着一个接收器(如单独留下相当长的一段时间)

我怀疑你会发现一个明确的清单某处。我还记得适合你的描述的唯一的广播是 SMS_RECEIVED 并通过 AlarmManager 任何触发和广播 PendingIntent

  

和保持手机清醒只要的onReceive()运行

这可能是 SMS_RECEIVED 有这种行为,但我不知道,一定的。该 AlarmManager 情景绝对不会。

  

(这当然不宜过长,以避免ANR)。

它需要远高于短,因为它会冻结你的用户界面,如果你碰巧有前景的活动。任何超过几毫秒的需要委托给服务,后台线程,比如我的 WakefulIntentService

  

作为奖励,我最近了解到,一些意图,标有FLAG_RECEIVER_REGISTERED_ONLY,只交付给动态注册接收器 - 是否有上市的意图任何地方

没有,超出源$ C ​​$ C。那些浮现在脑海中包括: ACTION_SCREEN_ON ACTION_SCREEN_OFF ACTION_BATTERY_CHANGED

I am not quite certain what the behavior of a BroadcastReceiver, registered in the manifest and enabled via PackageManager, is when the phone is asleep. The question arose because I need a receiver registered for broadcasts from WifiManager

<receiver
    android:name=".receivers.ScanResultsReceiver"
    android:enabled="false" >
    <intent-filter>
        <action android:name="android.net.wifi.SCAN_RESULTS" />
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

but what I want to know (as in links to the docs or some authoritative post in google groups) is which broadcasts are guaranteed to wake up a receiver when the phone has fallen asleep (as in left alone for quite some time) and keep the phone awake as long as onReceive() runs (which of course should not be too long to avoid ANR).
The receiver might well be the only component of the app running
As a bonus, I recently learned that some intents, flagged with FLAG_RECEIVER_REGISTERED_ONLY, are only delivered to dynamically registered Receivers - is there any place listing those intents ?

解决方案

I am not quite certain what the behavior of a BroadcastReceiver, registered in the manifest and enabled via PackageManager, is when the phone is asleep.

Most broadcasts do not wake up the device.

which broadcasts are guaranteed to wake up a receiver when the phone has fallen asleep (as in left alone for quite some time)

I doubt that you will find a definitive list somewhere. The only broadcasts that I can recall that fit your description are SMS_RECEIVED and any triggered via AlarmManager and a broadcast PendingIntent.

and keep the phone awake as long as onReceive() runs

It is possible that SMS_RECEIVED has this behavior, but I do not know that for certain. The AlarmManager scenario definitely does.

(which of course should not be too long to avoid ANR).

It needs to be far shorter than that, as it will freeze your UI, if you happen to have the foreground activity. Anything more than a couple of milliseconds needs to be delegated to a service with a background thread, such as my WakefulIntentService.

As a bonus, I recently learned that some intents, flagged with FLAG_RECEIVER_REGISTERED_ONLY, are only delivered to dynamically registered Receivers - is there any place listing those intents ?

No, beyond the source code. Ones that come to mind include: ACTION_SCREEN_ON, ACTION_SCREEN_OFF, and ACTION_BATTERY_CHANGED.

这篇关于当手机睡着的BroadcastReceiver行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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