Android Basic Knowledge broadcast Broadcast Detailed Explanation

  • 2021-07-24 11:45:02
  • OfStack

There are too many broadcasts in Android, so I will summarize it a little today.
According to the registration method, it is divided into two types:

1. Statically register broadcasts:
Static registration broadcast is to register broadcast in androidManifest. xml file. Suppose we want to achieve such an effect, click a button on an activity and send a broadcast, which pops up an toast and displays the word "static".

Look at the broadcast recipients first:


public class MyBroadcast extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context," Static ", Toast.LENGTH_LONG).show();
  }

}

Register in the manifest file:


  <receiver android:name="com.example.staticbroadcast.MyBroadcast" >
      <intent-filter>
        <action android:name="com.test.StaticBroadcast" />
      </intent-filter>
    </receiver>

Click Events in activity (Send Broadcast):


 this.static_btn.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent intent = new Intent();
        intent.setAction("com.test.StaticBroadcast");
        sendBroadcast(intent);
      }
    });

2. Dynamic registration:
Dynamic registration 1 is generally registered in the onStart () method in activity and unregistered in the onStop () method, with the following code:


public class MainActivity extends Activity {

  private Button static_btn;
  private Button dynamic_btn;
  private BroadcastReceiver myBroadcastReceiver;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.static_btn = (Button) this.findViewById(R.id.button1);
    this.dynamic_btn = (Button) this.findViewById(R.id.Button01);
    myBroadcastReceiver = new BroadcastReceiver(){

      @Override
      public void onReceive(Context context, Intent intent) {
        Toast.makeText(MainActivity.this," Hello, this is the dynamic broadcast! ", Toast.LENGTH_LONG).show();
      }

    };

//   this.static_btn.setOnClickListener(new OnClickListener() {
//
//     @Override
//     public void onClick(View v) {
//       Intent intent = new Intent();
//       intent.setAction("com.test.StaticBroadcast");
//       sendBroadcast(intent);
//     }
//   });

    this.dynamic_btn.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        // Send a broadcast 
        Intent intent = new Intent();
        intent.setAction("DynamicBroadcast");
        sendBroadcast(intent);
      }
    });
  }

  @Override
  protected void onStart() {
    super.onStart();
    // Registered broadcast 
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("DynamicBroadcast");
    registerReceiver(myBroadcastReceiver, intentFilter);

  }

  @Override
  protected void onStop() {
    super.onStop();
    // Cancel registration 
    unregisterReceiver(myBroadcastReceiver);
  }
}

Details about static registration:
android: exported= "true" This attribute indicates whether the broadcast receiver receives broadcasts from other App. If there is an intent-filter attribute, it defaults to true, otherwise it defaults to false.

Each broadcast receiver can accept multiple broadcast sources. If it is a static registration, you should do this:


  <receiver 
      android:exported="true"
      android:name="com.example.staticbroadcast.MyBroadcast" >
      <intent-filter>
        <action android:name="com.test.StaticBroadcast" />
        <action android:name="com.test.StaticBroadcast2"/>
      </intent-filter>
    </receiver>

In the broadcast receiver:


  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("com.test.StaticBroadcast")) {
      Toast.makeText(context, " Static ", Toast.LENGTH_SHORT).show();
    } else if (intent.getAction().equals("com.test.StaticBroadcast2")) {
      Toast.makeText(context, " Static 2", Toast.LENGTH_SHORT).show();
    }
  }

If it is dynamic registration, the registration method is as follows:


  @Override
  protected void onStart() {
    super.onStart();
    // Registered broadcast 
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("DynamicBroadcast");
    intentFilter.addAction("DynamicBroadcast2");
    registerReceiver(myBroadcastReceiver, intentFilter);

  }

The processing mode in the broadcast receiver is the same as static registration 1.

For information on how to use broadcast to communicate between activity and fragment, see my other blog about using Broadcast to communicate between android components

Original address: http://blog.csdn.net/u012702547/article/details/46955787


Related articles: