ACTION_BOOT_COMPLETED: System startup completed.
ACTION_PACKAGE_ADDED: adds a package to the system.
ACTION_PACKAGE_REMOVED: a package is removed from the system.
ACTION_PACKAGE_DATA_CLEARED: indicates that the package data is cleared.
ACTION_BATTERY_CHANGED: Battery level changed.
ACTION_BATTERY_LOW: Indicates that the battery is low.
ACTION_TIME_CHANGED: The system time is changed.
ACTION_DATE_CHANGED: The system date is changed.
ACTION_TIMEZONE_CHANGED: The system time zone is changed.
What are these actions up here? These are the system broadcasts.
Preface: What is broadcasting?
Broadcasting has system broadcasting and application broadcasting
System of radio
System he would send broadcast on many occasions, such as listed above listed above start to finish, add package, low battery, the system will send broadcast, it receives every APP on the phone, because now, of course, Android privacy is more and more strict, so some radio receive strict access control.
Application of radio
Apply custom broadcasts that can be used for interprocess communication. For example, an application can send a broadcast to transmit data to other applications, or to notify other applications of what you have accomplished.
One of the four components of Android, a means of communication, BroadcastReceiver is used to receive broadcast messages from other applications or systems.
This article focuses on the use of custom broadcasts.
One or two registration methods: dynamic and static
1. Dynamic registration
Registration:
IntentFilter filter = new IntentFilter(); filter.addAction("com.example.dynamic"); // registerReceiver(new DynamicReceiver(), filter);Copy the code
Unbundling:
unregisterReceiver(new DynamicReceiver());
Copy the code
Broadcast:
Intent = new Intent(); intent.setAction("com.example.dynamic"); Intent.putextra ("value", "Hello! ); sendBroadcast(intent);Copy the code
Inheriting the DynamicReceiver class from BroadcastReceiver:
public class DynamicReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent Intent) {toast.maketext (context, "dynamic broadcast:" + Intent.getStringextra ("value"), toast.length_long).show(); The d (getClass (). GetSimpleName () + "- the TEST", "dynamic radio"); }}Copy the code
2. Static registration
Manifest file configuration:
<receiver android:name=".broadcast.StaticReceiver">
<intent-filter>
<action android:name="com.example.static" />
</intent-filter>
</receiver>
Copy the code
Broadcast:
New Intent = new Intent("com.example.static"); Intent.putextra ("value", "Hello! ); // In-app broadcast, which solves the problem of Android 8.0 not accepting static registration broadcast. intent.setClassName(MainActivity.this, "com.example.myfirstproject.broadcast.StaticReceiver"); sendBroadcast(intent);Copy the code
Inheriting the BroadcastReceiver StaticReceiver class:
public class StaticReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent Intent) {toast.maketext (context, "static broadcast:" + Intent.getStringextra ("value"), toast.length_long).show(); Log.d(getClass().getSimplename (), "Static broadcast "); }}Copy the code
Note: If your Android version is 8.0+, static registration requires the following code to receive static registration broadcasts on your app’s Receiver.
intent.setClassName(MainActivity.this, "com.example.myfirstproject.broadcast.StaticReceiver");
If you need to send a broadcast to a statically registered broadcast receiver in another application, try adding the following code:
// This method is applicable to sending messages to broadcast receivers of other applications (specify application package name, specify class full class name)
Intent.setcomponent (New ComponentName(" ComponentName ", "ComponentName")). XXXReceiver"));
Official statement: Beginning with Android 8.0 (API Level 26), the system imposes additional restrictions on manifest-declared receivers. If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that do not target your app specifically).
Starting with Android 8.0 (API 26), there are additional restrictions on statically registering broadcast receivers in manifest files. It is recommended that you do not statically register broadcast receivers in manifest files.
Ii. General broadcasting & ordered broadcasting
In order to verify the difference between ordinary broadcast and ordered broadcast, we need to add a dynamic broadcast receiver Dynamic2Receiver. The code is the same as the dynamically registered DynamicReceiver above.
public class Dynamic2Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent Intent) {toast.maketext (context, "dynamic broadcast 2:" + Intent.getStringextra ("value"), toast.length_long).show(); Log.d(getClass().getSimplename () + "-test "," dynamic broadcast 2"); //abortBroadcast(); }}Copy the code
Next, dynamically register the two Receiver receivers, increasing the priority setPriority, and noticing the difference between the two Receiver priorities: Dynamic2Receiver is 1000 with a higher priority than DynamicReceiver-1000, that is, Dynamic2Receiver has a higher priority than DynamicReceiver to receive the broadcast.
IntentFilter filter = new IntentFilter(); filter.addAction("com.example.dynamic"); // Register broadcast reception filter.setpriority (-1000); registerReceiver(new DynamicReceiver(), filter); // Register for broadcast reception filter.setPriority(1000); registerReceiver(new Dynamic2Receiver(), filter); / / local radio localBroadcastManager = localBroadcastManager getInstance (this); filter.addAction("com.example.local"); localBroadcastManager.registerReceiver(new LocalReceiver(), filter);Copy the code
Also, if there is a registration, there is a unbinding:
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(new DynamicReceiver());
unregisterReceiver(new Dynamic2Receiver());
}
Copy the code
Let’s look at the differences between the different broadcast modes.
1. General radio
Intent intent = new Intent(); intent.setAction("com.example.dynamic"); Intent.putextra ("value", "Hello! ); sendBroadcast(intent);Copy the code
A standard broadcast mode in which the receiver is executed asynchronously.
Output the Log:
The 2021-08-06 13:24:09. 396, 11531-11531 / com. Example. Myfirstproject D/Dynamic2Receiver - TEST: Dynamic radio 2 2021-08-06 13:24:09. 424, 11531-11531 / com. Example. Myfirstproject D/DynamicReceiver - TEST: dynamic radioCopy the code
1. Orderly broadcast
Intent intent = new Intent(); intent.setAction("com.example.dynamic"); Intent.putextra ("value", "Hello! ); sendOrderedBroadcast(intent, null);Copy the code
Output the Log:
The 2021-08-06 13:26:21. 591, 12332-12332 / com. Example. Myfirstproject D/Dynamic2Receiver - TEST: Dynamic radio 2 2021-08-06 13:26:21. 756, 12332-12332 / com. Example. Myfirstproject D/DynamicReceiver - TEST: dynamic radioCopy the code
Use sendOrderedBroadcast to send ordered broadcasts. Recipients are executed in order of high priority to low priority. And we can intercept the broadcast at the high priority receiver, Dynamic2Receiver, so that the DynamicReceiver will not execute.
The following code is used to intercept the broadcast:
AbortBroadcast ();Copy the code
Add intercepting code output:
The 2021-08-06 13:25:35. 709, 12085-12085 / com. Example. Myfirstproject D/Dynamic2Receiver - TEST: dynamic radio 2Copy the code
On a whim, add abortBroadcast(); What’s the reaction?
The result is that the program does not crash but LogCat has an error log:
BroadcastReceiver trying to return result during a non-ordered broadcast
Copy the code
Local broadcasting
Why add a local broadcast when you already have a global broadcast?
This is because a BroadcastReceiver is designed to be global, to receive intents broadcast from its own application and from other applications. This also brings some security risks to the app. To solve this problem, Android adds local broadcast LocalBroadcastManager.
LocalBroadcastManager will only limit broadcasts to the current application. LocalBroadcastManager does not send broadcasts that leave your application, nor does it receive broadcasts from other applications, so you can safely broadcast sensitive information in LocalBroadcastManager without being intercepted by other applications. LocalBroadcastManager is more efficient than BroadcastReceiver because it does not require a cross-process mechanism. LocalBroadcastManager is only used in dynamic broadcast, static broadcast can not use LocalBroadcastManager.
So if you have no need to communicate with other applications for broadcasts, use LocalBroadcastManager first.
Familiar with the above dynamic registration to send and receive broadcasts, local broadcasting is very simple.
Local broadcasts are managed by calling a LocalBroadcastManager.
private LocalBroadcastManager localBroadcastManager;
Copy the code
Register local broadcast:
IntentFilter filter = new IntentFilter(); / / local radio localBroadcastManager = localBroadcastManager getInstance (this); filter.addAction("com.example.local"); localBroadcastManager.registerReceiver(new LocalReceiver(), filter);Copy the code
Unbind a local broadcast:
localBroadcastManager.unregisterReceiver(new LocalReceiver());
Copy the code
Call local broadcast:
Intent intent = new Intent(); intent.setAction("com.example.local"); Intent.putextra ("value", "Hello! ); localBroadcastManager.sendBroadcast(intent);Copy the code
Define a Receiver Receiver:
public class LocalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent Intent) {toast.maketext (context, "local broadcast:" + Intent.getStringextra ("value"), toast.length_long).show(); The d (getClass (). GetSimpleName () + "- the TEST", "local radio"); }}Copy the code
Above, actually with dynamic registration most of the code overlap. It is managed by the LocalBroadcastManager during registration, unbinding, and broadcast.
Output the Log:
The 2021-08-06 13:28:37. 218, 12332-12332 / com. Example. Myfirstproject D/LocalReceiver - TEST: local broadcastCopy the code
conclusion
So that’s a little bit about broadcasting. If you are still interested in how to use the system broadcast, you can check out my article # Two Ways to boot on Android, which introduces the system boot broadcast:
Portal: # Two ways for Android to boot from startup