1. 什么是广播
借助Intent发送广播,借助广播接收器接受广播。
在安卓中广播分为以下两类:
- 标准广播:完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条消息,这种广播的效率高,但无法被截断。
- 有序广播:同步广播,广播接收器依次接受广播,可以被截断。
2. 接受系统广播
1. 动态注册
2. 静态注册
在AndroidManifest.xml注册,程序未启动也能接受广播。
权限相关设置
3. 发送自定义广播
2021-02-23 16:47:49.081 30893-30893/? W/e.broadcasttes: Unexpected CPU variant for X86 using defaults: x86
2021-02-23 16:47:49.419 30893-30893/com.example.broadcasttest W/RenderThread: type=1400 audit(0.0:123): avc: denied { write } for name="property_service" dev="tmpfs" ino=7427 scontext=u:r:untrusted_app:s0:c145,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.example.broadcasttest
2021-02-23 16:47:49.422 30893-30949/com.example.broadcasttest W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2021-02-23 16:47:49.592 30893-30893/com.example.broadcasttest W/e.broadcasttes: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-02-23 16:47:49.592 30893-30893/com.example.broadcasttest W/e.broadcasttes: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-02-23 16:47:49.705 30893-30946/com.example.broadcasttest W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-02-23 16:47:49.773 30893-30946/com.example.broadcasttest W/Gralloc3: mapper 3.x is not supported
自定义广播报错,待解决。
4. 使用本地广播
LocalBroadcastManager