Create a broadcast receiver: Simply new class inherits from Broadcastreceiver, then overrides OnReceive method
There are two ways to register a broadcast:
1. Static registration: That is, register in the manifest file, for example, the following listen to call broadcast:
<receiver android:name= ". Mybroadcastreceiver "> <intent-filter> <action android:name=" android.intent.action.NEW_ Outgoing_call "/> </intent-filter></receiver>
1. Dynamic registration: That is, registering in the code, for example:
New= "Android.intent.action.NEW_OUTGOING_CALL"new intentfilter (action); Registerreceiver (receiver,intentfilter);
Dynamically registered broadcasts need to be de-registered in the OnDestroy method
Unregisterreceiver (receiver);
Note: Dynamically registered broadcasts rely on the lifecycle of the component that registers the broadcast
The static registration of the broadcast once registered, there has been
The type of broadcast is consistent with the type that the broadcast recipient configures in the manifest file, or the broadcast will not be received
Broadcast divided into ordered broadcast and disorderly broadcast
Out-of-order broadcasts: sent through Sendbroadcast (intent), cannot be intercepted, all broadcast receivers can be received at almost the same time, with no sequence to speak
Orderly broadcast: Sent through Sendorderedbroadcast (Intent,null), after the broadcast, only one broadcast receiver can be received at the same time, the execution is complete, the broadcast can continue to pass
A high-priority broadcast receiver receives broadcasts first, and can also use Abortbroadcast () to terminate the broadcast.
The priority can be set by the android:priority= "" parameter, the range is -1000~1000
If the priority of the two broadcast receivers is the same, the component registered first receives the broadcast
If two applications listen to the same broadcast event and set the priority, the first installed app receives the broadcast
Tip: If you need a broadcast recipient to receive a broadcast anyway, whether it's the lowest priority or a high-priority termination
Can call Sendorderedbroadcast (intent,null,receiver,null,0,null,null);
The first parameter is intent, and the third parameter is the specified broadcast receiver object
The Broadcastreceiver of Android development