Introduction to Android Broadcast Mechanism

Types of

Broadcasts in Android are mainly divided into two categories: standard broadcasts and ordered broadcasts.

  • A standard broadcast is a completely asynchronous broadcast. After it is sent, all broadcast receivers receive the broadcast almost at the same time. This type of broadcast cannot be truncated.
  • Ordered broadcast is a synchronous broadcast, and only one broadcast receiver can receive the broadcast message at the same time after the broadcast is sent. Broadcast receivers with higher priority receive broadcast messages first and can truncate the broadcast being delivered.

receive broadcast

There are two registration methods: registration in code (dynamic registration) and registration in AndroidManifest.xml (static registration)

dynamic registration

  1. Create a broadcast receiver class, inherit from BroadcastReceiver, and override the onReceive() method of the parent class.
  2. Create an instance of intentFilter and add the corresponding action to it (if you want to listen to any broadcast, add the corresponding action)
  3. Finally, call the registerReceiver(BroadcastReceiver receiver, IntentFilter intentFilter) method to complete the registration.
  4. Dynamically registered broadcast receivers must be unregistered.
    If unregisterReceiver() involves some sensitive operations, you need to declare permissions in the configuration file, such as < uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>

static registration

  1. With the dynamic registration step 1, create a broadcast receiver class
  2. Register in AndroidManifest, example:
<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <actoin android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

android.intent.action.BOOT_COMPLETED is a broadcast sent after the system is powered on

send broadcast

send standard broadcast

Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);

send ordered broadcast

Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendOrderedBroadcast(intent, null);

It can be seen that there is only one line difference between ordered broadcasting and standard broadcasting. sendOrderedBroadcast() receives two parameters, the first is still an Intent, and the second is a string related to permissions.
The receiver's priority-related settings are as follows:

<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="100">
        <actoin android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

The broadcast receiver with high priority can receive the broadcast first, and the abortBroadcast() method can be called to truncate the broadcast, and subsequent broadcast receivers will no longer be able to receive the broadcast.

Use local broadcast

  1. Local broadcasts cannot be received by static registration.
  2. Local broadcasts do not leave our program without worrying about leaking confidential data
  3. Other programs cannot send broadcasts to the inside of our program
  4. Sending local broadcasts is more efficient than sending system global broadcasts.
    Usage: mainly use a LocalBroadcastManager to manage the broadcast. Such as:
LocalBroadcastManager localBroadcastManager;
localBroadcastManager = LocalBroadcastManager.getInstance(this);
//发送本地广播
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
//注册接收本地广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
LocalReceiver localReceiver = new LocalReceiver();//先定义好LocalReceiver类
localBroadcastManager.registerReceiver(localReceiver, intentFilter);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325442123&siteId=291194637