Android 广播接收者
1.简介
在现实生活中,很多人会收听广播,例如,通过广播收听音乐。同样在Android系统中也内置了很多广播,例如手机开机会发送一条广播,电量不足也会发送一条广播等等。
为了监听这些广播事件,Android系统提供了一个BroadcastReceiver(广播接收者)组件,该组件可以监听来自系统或者应用程序的广播。
2.例子
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
tools:context=