Introduction
Broadcast (broadcast) is one of the four major components of Android for inter-process/thread communication.
The biggest feature of broadcasting is that the sender does not care whether the receiver receives the data or how the receiver processes the data. It is only responsible for "speaking" regardless of whether you "listen or not".
Broadcasts can come from the system, for example, the Android system sends broadcasts when various system events occur (when the system starts up or when the device starts charging).
It can also come from other applications. For example, applications can also send custom broadcasts to notify other applications to accept content (update data) that they may be interested in.
Table of contents
Classification of Broadcasting
Classification of Broadcasting
Classified by sending method
-
standard broadcast
It is a "completely asynchronous execution" broadcast without any sequence. All broadcast receivers receive this broadcast message almost at the same time, which is highly efficient and cannot be truncated.
-
orderly broadcast
It is a "synchronous execution" broadcast, which has a sequence. Only one receiver can receive this broadcast message at the same time. The broadcast receiver with higher priority can receive the broadcast message first, and the previous broadcast receiver can also intercept the current broadcast message. The transmitted broadcast, so that the subsequent broadcast receiver cannot receive the broadcast message.
Classified by registration method
-
static broadcast
Listening occurs regardless of whether the application is active or not. Each trigger will create a new Receiver object.
-
dynamic broadcast
Register in the code. Note that the dynamically registered broadcast must be unregistered, usually onDestroy()
by calling unregisterReceiver()
the method in the method.
The same Receiver will be used from creation until it is unregistered, no matter how many times the broadcast is triggered.
Classify by definition
-
system broadcast
There are multiple system broadcasts built in the Android system, and each system broadcast has a specific IntentFilter, which mainly includes a specific Action. After the system broadcast is sent out, it will be received by the corresponding BroadcastReceiver. System broadcast is automatically sent out by the system when a specific event occurs inside the system.
-
custom radio
Broadcasts defined by application developers themselves.
Classified by scope
-
global broadcast
Broadcasts sent out can be received by any other application, or can receive broadcasts from any other application.
-
local broadcast
Broadcasts can only be delivered within the application, and broadcast receivers can only receive internal broadcasts, and cannot accept broadcasts from other applications.
Use of broadcasting
Create broadcast receiver
To use broadcasting, we need to create a BroadcastReceiver (broadcast receiver) first, and directly inherit BroadcastReceiver to create a subclass and implement onReceive()
the method , as shown in the following sample code.
public class MyReceiver extends BroadcastReceiver { // custom action private static final String ACTION = "com.jeanboy.broadcast.MyReceiverFilter"; @Override public void onReceive(Context context, Intent intent) { //TODO: Receive broadcast for processing } }
static broadcast
When using broadcasting, it also needs to be defined in the AndroidMainfest file, that is, registering static broadcasting.
<receiver android:name=".ui.broadcast.MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <!-- For example: Receive system boot broadcast --> <action android :name="android.intent.action.BOOT_COMPLETED" /> <!-- For example: Receive a custom broadcast --> <action android:name="com.jeanboy.broadcast.MyReceiverFilter" /> </intent-filter > </receiver>
Setting enabled above to true means that broadcast information can be received. Exported is true means it can receive the broadcast information sent by external APK.
dynamic broadcast
The use of dynamic broadcasting does not need to be defined in the AndroidMainfest file, it only needs to be registered in the code.
// Create a broadcast MyReceiver myReceiver = new MyReceiver(); // Create an IntentFilter IntentFilter intentFilter = new IntentFilter(); // For example: add a system broadcast action to accept network changes intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); // For example: add from Defined action intentFilter.addAction(MyReceiver.ACTION); // register broadcast registerReceiver(myReceiver, intentFilter); // cancel broadcast unregisterReceiver(myReceiver);
send broadcast
Sending broadcasts is relatively simple, regardless of static broadcasts or dynamic broadcasts, the method is as follows (system broadcasts are automatically sent by the Android system, which is out of the scope of this article).
// Create an Intent Intent intent = new Intent(); // For example: add a custom action intent.setAction(MyReceiver.ACTION); // Send a broadcast sendBroadcast(intent);
Static broadcasts in Android 8.0
Since Android 8.0 abolishes most static broadcasts, some parts of the code need to be modified.
The sending broadcast part requires setup ComponetName
.
Intent intent = new Intent(MyReceiver.ACTION); // ComponentName("Custom broadcast package name", "Custom broadcast path") ComponentName component = new ComponentName("com.jeanboy.app.broadcast", "com .jeanboy.app.broadcast.MyReceiver"); intent.setComponent(component); sendBroadcast(intent);
broadcast with permission
Security issues that may arise from using broadcasts:
-
If other applications listen to our broadcast, it will cause data leakage of our application;
-
If another application pretends to be our application to send broadcasts, it will frequently start our broadcast receiving program, causing confusion or even crash of our application.
In order to avoid the above security problems, Android provides us with a permission mechanism.
First, you can add permissions in the AndroidMainfest file when registering static broadcasting.
<manifest ...> <!-- Customize your own permissions --> <permission android:name="com.jeanboy.permissions.MY_BROADCAST"/> <!-- Use custom permissions --> <uses -permission android:name="com.jeanboy.permissions.MY_BROADCAST"/> < application ...> <!--Add permission--> <receiver android:name=".ui.broadcast.MyReceiver" android:permission ="com.jeanboy.broadcast.MY_BROADCAST" android:enabled="true" android:exported="true"> <intent-filter> <!-- For example: receive a custom broadcast --> <action android:name= "com.jeanboy.broadcast.MyReceiverFilter"/> </intent-filter> </receiver> </application> </manifest>
Then when we send a broadcast, we can specify a permission for it, and only the app with this permission can receive the broadcast, as shown below:
// Create Intent Intent intent = new Intent(); // For example: add a custom action intent.setAction(MyReceiver.ACTION); // Send broadcast, add permission sendBroadcast(intent, "com.jeanboy.permissions.MY_BROADCAST" );
local broadcast
The BroadcastReceiver described above is used to transfer messages between applications. It is essentially cross-process and may be intercepted by other applications.
The LocalBroadcast (local broadcast) is used to transmit messages within the application, which is more efficient than BroadcastReceiver. It is only valid within the application and does not need to consider security issues.
The creation of the local broadcast is still to inherit BroadcastReceiver to create a subclass and implement onReceive()
the method . Use LocalBroadcastManager to perform related operations when registering, sending, and unregistering broadcasts.
// Create a broadcast MyReceiver myReceiver = new MyReceiver(); // Create an IntentFilter IntentFilter intentFilter = new IntentFilter(); // For example: add a custom action intentFilter.addAction(MyReceiver.ACTION); // Register a local broadcast LocalBroadcastManager.getInstance (this) .registerReceiver(myReceiver, intentFilter); // Send broadcast Intent intent = new Intent(MyReceiver.ACTION)); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); // Cancel local broadcast LocalBroadcastManager.getInstance( this).unregisterReceiver(myReceiver);