广播接收者(BroadCastReceiver)是Android的四大组件之一.
广播事件处理属于系统级的事件处理(一般事件处理为view级的).
广播事件机制是应用程序之间的通信(进程之间的通信).
context
- sendBroadcast(Intent intent): send general broadcast
- sendOrderedBroadCast(Intent Intent): Send an ordered broadcast
- registerReceiver(receiver, intentFilter): Register a broadcast receiver
- unRegisterReceiver(receiver): Unregister the broadcast receiver
BroadCastReceiver
- onReceive(Context context, Intent intent): Callback when the broadcast is received
- abortBroadcast(): interrupt the continued propagation of broadcast
- boolean isOrderedBroadcast(): Determine whether it is an ordered broadcast
Broadcast receivers must inherit the BroadcastReceiver class.
Register the broadcast receiver:
- Static registration
Configuration related files: AndroidManifest.xml
Add:
Application scenario: the entire program life cycle. - Dynamic registration
Application scenario: some Activity or Service
Broadcast classification:
1. General broadcast
context.sendBroadCast(Intent intent)
- General broadcast: when multiple people answer, it is disorderly and will not be interrupted
2. Orderly broadcast
context.sendOrderedBroadcast(Intent intent)
- Orderly broadcast: When multiple people answer, orderly, it will be interrupted