少于 1 分钟阅读 次阅读

广播接收器(BroadcastReceiver)介绍

BroadcastReceiver 是 Android 四大组件之一,用于在应用之间或系统与应用之间接收广播消息。它可以响应系统事件(如开机完成、网络变化、电量低等)或应用自定义事件。


示例详解

  • 在 AndroidManifest.xml 的中声明。

1. 自定义广播接收器

<receiver
    android:name="com.android.api.sleepwake.SleepReceiver"
    android:exported="false" />
<receiver
    android:name="com.android.api.sleepwake.WakeReceiver"
    android:exported="false" />

特点:

  • exported="false":表示此接收器只能接收来自本应用的广播
  • 没有 <intent-filter>:通常通过代码动态注册,或接收显式 Intent(指定了完整类名)
  • 更安全,外部应用无法触发

使用场景:

  • 应用内部组件间通信
  • 通过代码注册,在特定生命周期内接收广播

2. 带过滤器的广播接收器

<receiver
    android:name="com.android.api.sleepwake.SleepWakeBootReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

特点:

  • exported="true":允许接收系统或其他应用的广播
  • <intent-filter>:监听特定的广播动作
  • 监听系统开机完成事件

Intent-filter 详解

作用

<intent-filter> 声明了接收器感兴趣的广播类型。

常见系统广播 Action

<!-- 开机完成 -->
<action android:name="android.intent.action.BOOT_COMPLETED" />

<!-- 电量低 -->
<action android:name="android.intent.action.BATTERY_LOW" />

<!-- 充电状态变化 -->
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />

<!-- 网络状态变化 -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

<!-- 时区变化 -->
<action android:name="android.intent.action.TIMEZONE_CHANGED" />

<!-- 屏幕开启/关闭 -->
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />

留下评论