4.3.1 A Gentle Introduction to BroadcastReceiver
Category Android Basic Tutorial
Introduction to This Section
In this section, we will learn about the third of the four major components in Android: BroadcastReceiver. I was just thinking about how to start, so I flipped through two Android basic books I had on hand and found that neither of them introduced BroadcastReceiver. I don't know if it's a coincidence or if the authors think it's not commonly used and not worth discussing! However, they won't talk about it, but I will, and I will talk about it in detail! Okay, let's start with this section's content~ PS: By the way, on the Android official website, when you click on API Guides -> App Components, you won't find the trace of BroadcastReceiver. Well, just search for BroadcastReceiver directly, and the corresponding document address is: BroadcastReceiver
1. What is BroadcastReceiver?
>
Answer: Broadcast is directly translated as broadcasting. Let's give a vivid example to help us understand BroadcastReceiver. I remember when I was studying, each class would have a loudspeaker hanging on the wall to broadcast some notifications, such as moving books at the beginning of the school year, and the broadcast would say: "Each class finds a few students to pick up books at the academic affairs office." After this broadcast is sent out, all students will receive this broadcast notification at the same time, but not every student will go to move the books. Generally, the ones who go to move the books are the "strong men" in the class. After receiving this broadcast, this group of "strong men" will set out to bring the books back!
2. Two Types of Broadcasts:
3. Receiving System Broadcasts
1) Two Ways to Register Broadcasts
>
As mentioned earlier, the system will send corresponding system broadcasts at certain times. Next, let's allow our APP to receive system broadcasts. Before receiving, we also need to register a broadcast receiver for our APP! And there are two methods for registration: dynamic and static!
Let's demonstrate the usage and differences of the two through code:
2) Dynamic Registration Example (Listening for Network Status Changes)
Code Example:
Effect Picture:
Well, at the beginning, there was no network connection, that is, WiFi was not turned on. After a while, after clicking to turn on WiFi, a Toast prompt appeared~ It's also very simple to implement!
Code Implementation:
Customize a BroadcastReceiver and complete the broadcast processing tasks in the onReceive() method, such as the prompt Toast information here: MyBRReceiver.java
public class MyBRReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Network status has changed~", Toast.LENGTH_SHORT).show();
}
}
MainActivity.java Dynamically register the broadcast:
public class MainActivity extends AppCompatActivity {
MyBRReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Core part of the code:
myReceiver = new MyBRReceiver();
IntentFilter itFilter = new IntentFilter();
itFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(myReceiver, itFilter);
}
// Don't forget to cancel the broadcast~
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
}
Dynamic registration is simple, right? But dynamic registration has a disadvantage, which is that it requires the program to be started to receive broadcasts. If we need the program to be able to receive broadcasts without being started, then we need to register a static broadcast!
3) Static Registration Example (Receiving Boot Broadcast)
Code Example:
There is no schematic diagram here~, just look at the code implementation directly~
Code Implementation:
1. Customize a BroadcastReceiver and override onReceive to complete transaction processing
public class BootCompleteReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, "Boot completed~", Toast.LENGTH_LONG).show();
}
}
2. Register this BroadcastReceiver in AndroidManifest.xml and add a boot broadcast intent-filter!
By the way, don't forget to add the android.permission.RECEIVEBOOTCOMPLETED permission!
```xml <receiver android:name=".BootCompleteReceiver"> <intent-filter> <action android
1.8 Project-Related Analysis (Various Files, Resource Access)
2.5.4 Basic Use of AutoCompleteTextView (Auto-Complete Text Box)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
-
6.3.1 Data Storage and Access - An Introduction to SQLite Database
6.3.2 Data Storage and Access - Another Look at SQLite Database
7.1.1 Android Network Programming Essentials and Http Protocol Learning
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (Part One)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Two)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Three)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Four)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (Part Five)