Broadcasts can be categorized into two types: Normal broadcasts and Ordered broadcasts. Ordinary broadcasts are completely asynchronous and can be received (logically) by all receivers at the same time. The message delivery efficiency is relatively high, but there are disadvantages: the receiver cannot pass the result of the processing to the next receiver, and the propagation of the broadcast Intent cannot be terminated. An ordered broadcast is performed according to the priority declared by the receiver (declared in the Android: Priority attribute of the intent-filter element, the higher the number, the higher the priority. The value ranges from -1000 to 1000. You can also call setPriority() on the IntentFilter object) to receive broadcasts in turn. For example, if A is higher than B, and B is higher than C, the broadcast is first transmitted to A, then to B, and finally to C. After receiving A broadcast, A can save data to the broadcast. When the broadcast is transmitted to B,B can get the data saved by A from the broadcast.

Send broadcast

// Send a normal broadcast context.sendbroadcast (); -- -- -- -- -- -- -- -- -- -- - line -- -- -- -- -- -- -- -- -- -- -- - / / send orderly broadcast Context. SendOrderedBroadcast ();Copy the code

Life cycle:

Method of registration for broadcast

Method one, static registration

// <receiver android:name=".broadcast.MyBroadcastReceiver">

            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="test"</intent-filter> </receiver>Copy the code

Mode 2. Dynamic registration Must first apply for the following permissions:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Copy the code

The code registers as follows:

registerReceiver(new MyBroadcastReceiver(),new IntentFilter("test"));
Copy the code

Note: Active broadcast is best registered in the Activity’s onResume() and unregistered in onPause().

Static registration and dynamic registration difference

  • Dynamically registered broadcasts are not resident broadcasts, which means that broadcasts follow the activity’s life cycle. Note: Remove the broadcast receiver before the activity ends. Static registration is resident, which means that when an application is closed, it is automatically run by system calls if a message is broadcast.

  • When the broadcast is ordered, 1 receivers with higher priorities receive first; 2 Receivers with the same priorities receive dynamically before receivers with the same priorities; static receivers scan first before receivers scan later; dynamic receivers register first before receivers register later.

  • When the broadcast is a normal broadcast: 1. The dynamic broadcast receiver has priority over the static broadcast receiver. 2.

Summary:

  • In Android, if you want to send a broadcast, you must use sendBroadcast() to send the interested broadcast receiver to the system;
  • To use broadcasts, you must have an Intent object that must set its Action object.
  • To use a broadcast, the broadcast object must be explicitly specified in the configuration file.
  • Each time a broadcast is received, an object that receives the broadcast is regenerated
  • Try not to deal with too much logic in BroadCastReceiver. It is recommended that activities or services handle complex logic
  • If you register in androidmanifest.xml, you will also receive a broadcast when the application is closed. Registering in code does not cause this.

Additional: BroadcastReceiver history of the most comprehensive analysis