“This is the 27th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
The two most commonly used components in the development of Android, Activity and Service, are introduced not in order, but in accordance with the use of the scene. In this article, we introduce Broadcast, also known as Broadcast Receiver, which is usually used. It is most often used with activities and services, and is described below.
Broadcast Receiver definition
BroadcastReceiver is one of the four components of Android. As its name implies, BroadcastReceiver delivers messages through broadcasting. In essence, it is a global listener that can monitor various broadcasts and realize communication between different components. The biggest characteristic of broadcast is that the sender does not care whether the receiver receives the data or not, nor how the receiver processes the data. In this way, the complete decoupling of the receiver and receiver is achieved. An BroadcastReceiver refers to an Intent that an application can send and receive an BroadcastReceiver, as well as receive broadcasts from the system or other applications, or send them to other applications.
The type of BroadcastReceiver
We use ordinary broadcast and ordered broadcast more often in development, ordinary broadcast corresponds to ordered broadcast, also called out-of-order broadcast.
Ordinary radio
Also known as out-of-order broadcast, this broadcast is completely asynchronous. All broadcastReceivers that match the BroadcastReceiver Intent can receive the broadcast simultaneously, in any order, and send it via context.sendbroadcast (). This broadcast is more efficient, but the disadvantage is that the receiver cannot pass the processing results to the next receiver and cannot terminate the broadcast in mid-stream.
Orderly broadcast
This is a kind of synchronous execution radio, through the Context, sendOrderedBroadcast () method to send, this after the issue of the broadcast through the receiver’s intent – filter in the android: specify the attribute to set the priority, The priority ranges from -1000 to 1000. The higher the priority is, the higher the priority is. Use the setResult() method to send the result to the next receiver. Causes the broadcast not to be passed to the next receiver.
The use of the BroadcastReceiver
When using broadcasts, you need to register them in the manifest, just like using activities and services. Of course, broadcasts can also be registered dynamically in the code.
To write a simple system broadcast listener, create a class that extends broadcast: MyBroadCastReceiver
public class MyBroadCastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent Intent) {toast.maketext (context," network status changed ~", toast.length_short).show(); }}Copy the code
Here print a simple toast prompt and write a class for the broadcast to take place, MainActivity:
public class MainActivity extends AppCompatActivity { MyBRReceiver myReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyReceiver = new MyBroadCastReceiver(); IntentFilter itFilter = new IntentFilter(); itFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); registerReceiver(myReceiver, itFilter); } // Override protected void onDestroy() {super.ondestroy (); unregisterReceiver(myReceiver); }}Copy the code
A brief description of the difference between dynamic registration and static registration, dynamic registration is required to start the program to accept broadcast, static registration, the program does not start can also receive.
After completing the above code, simply switch network status and receive a broadcast. Isn’t it very simple? In fact, broadcasting is relatively simple to use. Here is the introduction of the use of the system’s own broadcast, so we custom broadcast how to send it, the code is very simple:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_send = (Button) findViewById(R.id.btn_send); btn_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBroadcast(new Intent("com.test.broadcasttest.MY_BROADCAST")); }}); }}Copy the code
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.test.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
Copy the code
Let’s write the same receiver:
public class MyBroadcastReceiver extends BroadcastReceiver { private final String ACTION_BOOT = "com.test.broadcasttest.MY_BROADCAST"; @Override public void onReceive(Context context, Intent Intent) {if(action_boot.equals (intent.getAction())) toast.maketext (context, "received ", toast.length_short).show(); }}Copy the code
Considerations for using BroadcastReceiver
Broadcast is easy to use, but there is one thing that must be taken care of: do not do too many time-consuming operations in the receive method (i.e. onReceiver), otherwise it will cause ANR, because new threads cannot be opened to execute in the broadcast.
Broadcasting actually is more widely used in the system, mainly introduced here are the global radio, not only your application can receive, if other applications also know you set the action, can also be received in the receiving method, here there will be some performance loss and security issues, radio, of course, there are Local radio (Local Broadcast). Local broadcast is a broadcast that can only be used in this application. I won’t introduce it here. If you are interested, you can check the relevant information online.
The broadcast is introduced here, there is anything wrong, please correct, welcome to leave a comment.