What is a BroadcastReceiver?

BroadcastReceiver Is a global system listener that listens for Broadcast messages in the system. Therefore, it is convenient for communication between system components.

BroadcastReceiver is a system-level listener. It has its own process and is activated when a Broadcast matching it is sent as an Intent.

Although BroadcastReceiver is one of the four main components of Android, it has its own declaration cycle, but it is different from Activity and Service. When a BroadcastReceiver is released as an Intent, the system automatically creates an instance of the BroadcastReceiver and triggers its onReceive() method. When the onReceive() method is executed, the instance of the BroadcastReceiver is destroyed. Although it alone to enjoy a separate process, but is not without limits, if BroadcastReceiver onReceive () method can not be completed in 10 seconds, the Android system will think the BroadcastReceiver object no response, Then the pop-up ANR (No Response) dialog box, so don’t in the BroadcastReceiver. OnReceive () method is executed within some time consuming operation.

If a long-running operation needs to be performed based on BroadcastReceiver content, it is generally considered to start a Service with an Intent to complete the operation, rather than start a new thread in a BroadcastReceiver. If the BroadcastReceiver exits before the BroadcastReceiver’s child thread ends, the BroadcastReceiver will be marked as an empty thread. The BroadcastReceiver terminates the thread with the lowest priority, and the null thread is the lowest priority, which can cause the BroadcastReceiver’s child threads to fail to complete.

The types of BroadcastReceiver

  • SendBroadcast () : sends ordinary broadcast.
  • SendOrderedBroadcast () : sends ordered broadcasts.

How to use BroadcastReceiver

Manifest files are registered

<receiver android:name=".HelloBroadcastReceiver">  
    <intent-filter android:priority="100">  
        <action android:name="com.cl.apksample.HelloBroadcastReceiver" />  
    </intent-filter>  
</receiver> 
Copy the code

HelloBroadcastReceiver

public class HelloBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent Intent) {// Determine the action log. d of the received Broadcast."wxl"."intent.getAction()====" + intent.getAction());  
        if (intent.getAction().equals("com.cl.apksample.HelloBroadcastReceiver")) {  
            Toast.makeText(context, "The Broadcast message is received as follows:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show(); }}}Copy the code

Send Broadcast

Intent broadcast = new Intent();                  
broadcast.setAction("com.cl.apksample.HelloBroadcastReceiver");         
sendBroadcast(broadcast);
Copy the code

Note:

Broadcast in Android is divided into two types: standard broadcast and ordered broadcast Standard broadcast Standard broadcast is a completely asynchronous broadcast. After the broadcast is sent, all the broadcast receivers will receive the broadcast at the same time. It has high efficiency and cannot be truncated. Orderly broadcast Orderly broadcast is a kind of synchronous execution, on the radio after the same time only one radio receiver can receive, the higher priority (the higher the value, the greater the priority) radio receiver would receive priority, when the high priority onReceiver radio receivers () method after the operation, broadcast will continue to pass, And the front receiver can choose to truncate the broadcast, so that the back receiver cannot receive the broadcastCopy the code
1. There are different levels of the broadcast. The value of the broadcast level ranges from -1000 to 1000. 2. The same level receives the broadcast randomly, and then the lower level receives the broadcast; 3. The same level of reception is random in order. If the first received one truncates the broadcast, the other recipient of the same level cannot receive the broadcast. AbortBroadcast () 4, abortBroadcast() 4, abortBroadcast() 4, abortBroadcast() 4, abortBroadcast() 4, abortBroadcast() 4, abortBroadcast() 4. 5. Experimental phenomenon: In the broadcast sent by this method, in the code registration mode, the order of receiving broadcast is: marked priority, code registration, no priority; If none is prioritized, the code register is received first.Copy the code
  • Active broadcast is best registered in the Activity’s onResume() and unregistered in onPause().

    Reason: For dynamic broadcast, there must be a register must be deregistration, otherwise it will lead to memory leaks

Repeated registration and cancellation are not allowedCopy the code

The Activity lifecycle is as follows:

  • onCreate() & onDestory()
  • onStart() & onStop()
  • onResume() & onPause()

Register onResume() and unregister onPause() because onPause() must be executed before the App dies, ensuring that broadcasts are unregistered before the App dies and preventing memory leaks.

1 Does not register onCreate() & onDestory() or onStart() & onStop() because: When the system attempts to reclaim resources from an Activity because it is running out of memory (memory is required for higher-priority applications, see the red box above), the Activity is destroyed after executing onPause(), and some lifecycle methods onStop() and onDestory() are not executed. When you return to the Activity, you start with the onCreate method. 2. If we put the broadcast logout in onStop(), onDestory(), we may not execute onStop(), onDestory() after the Activity is destroyed, which will cause memory leaks. 3 However, onPause() must be executed to ensure that the broadcast is unregistered before the App dies, thus preventing memory leaks.Copy the code

The difference between the two registration methods

There are five main types of broadcasting:

  • Normal Broadcast
  • System Broadcast
  • Ordered Broadcast
  • Sticky Broadcast
  • Local Broadcast in App

LocalBroadcastManager

All broadcasts sent and received are global broadcasts of the system. That is, broadcasts sent and received by other applications can be received by other applications, which may cause insecurity

Therefore, in some cases, a local broadcast mechanism can be used, where broadcasts issued by this mechanism can only be transmitted within the application, and the broadcast receiver can only receive broadcasts issued within the application itself

It is important to note that local broadcasts cannot be received through static registration, because static registration is mainly used to receive broadcasts without the application being started, while local broadcasts are sent by the application itself, which must be started.

Use private Permissions

Android BroadcastReceiver

Reference:

Android BroadcastReceiver- Wu Xiaolong

BroadcastReceiver is the most comprehensive overview of Android