BroadcastReceiver overview

A BroadcastReceiver, one of Android’s four main components, listens to, receives, and responds to broadcast messages sent by apps. For example, communication between different Components of Android (including in-app/inter-app), multi-threaded communication, and communication with Android system under specific circumstances, etc. Broadcasting in Android uses the design pattern observer pattern: message-based publish/subscribe event model, so there are three roles in broadcasting: message subscriber (broadcast receiver), message publisher (broadcast publisher), and message center (AMS, or Activity Manager Service).

Broadcast receiver registration

Static registration

Static registration is a global broadcast. When this App starts for the first time, the system will automatically instantiate the registered broadcast class and register it in the system. How to register: declare it in androidmanifest.xml with tags

  • Registration of radio
 <receiver android:name="com.yz.listener.BgReceiver" >

    <intent-filter>

        <action android:name="1" />

        <action android:name="2" />

    </intent-filter>

 </receiver>

Copy the code

Note: The registered action must be consistent with the recipient

  • The BroadcastReceiver inherits BroadcastReceiver as a broadcast class, and the onReceive method that implements it accepts broadcasts and implements callbacks.
public class BgReceiver extends BroadcastReceiver {



    @Override

    public void onReceive(Context context, Intent intent) {

       

    }

}

Copy the code
  • A broadcast of announcements without data by the publisher
SendBroadcast (new Intent("1"))// Publish a broadcast with action "1"

Copy the code

Broadcast with data notification

Intent receiveIntent = new Intent();

receiveIntent.setAction("2");

receiveIntent.putExtra("name", "yz");

this.sendBroadcast(receiveIntent);

Copy the code
  • Radio response
public class BgReceiver extends BroadcastReceiver {



    @Override

    public void onReceive(Context context, Intent intent) {

         switch (intent.getAction().trim()) {

            case "1":

// Notification broadcast with no parameters

Break;

      case "2":

// Notification broadcast with parameters

        String name = intent.getStringExtra("name");

Break;

    }

}

Copy the code

Note: Broadcasts are distinguished by registered actions. To send a broadcast, you must first register its action.

Dynamic registration

Dynamically registering for broadcasts is registering where you need to receive broadcasts. For dynamic broadcast, there is a registration must be deregistered, otherwise it will lead to memory leaks, repeated registration, repeated deregistration is not allowed. Generally, you register the broadcast in onCreate and unregister the broadcast in onDestory.

  • Defining broadcast Receivers
 private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override

        public void onReceive(Context context, Intent intent) {

            switch (intent.getAction().trim()) {

                case "1":                    

                    break;

                case "2":                   

                    break;

            }

        }

    };

Copy the code
  • Registration of radio
IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction("1");

intentFilter.addAction("2");

registerReceiver(mReceiver, intentFilter);

Copy the code
  • Cancellation of radio
unregisterReceiver(mReceiver);

Copy the code

Note: The registered broadcast action should not be repeated. It must be unique, otherwise it will be received by other broadcast.

As for orderly broadcast and sticky broadcast, I will not describe them all. Sticky broadcast is no longer recommended. Orderly broadcast is similar to ordinary broadcast, in which the value of Priority is added during registration. If the Priority attribute is the same, dynamically registered broadcasts take precedence. SendOrderedBroadcast (Intent) is used to send an ordered broadcast. . Now EventBus has replaced Intent, Broadcast, and Hander. As one of the four components of Android, we still need to know.