Learning BroadcastReceiver Summary - continuous update

Android provides broadcast sending and receiving mechanisms. The broadcaster is Android system and Application. The Action constant of Android system broadcast is defined in the Intent class (as shown below). For details, please refer to Android API documentation.

public class Intent
extends Object implements Parcelable, Cloneable

java.lang.Object
   ↳	android.content.Intent

The general steps to monitor the dynamic registration mode of Android system or Application broadcast are as follows:

  1. Define a XXXReceiver class that inherits BroadcastReceiver
  2. Override the onReceive(Context context, Intent intent) method of its parent class in the XXXReceiver class
  3. new an object of XXXReceiver class where needed
  4. Call the registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) method of context where appropriate
  5. Call unregisterReceiver(BroadcastReceiver broadcastReceiver) method of context where appropriate

After the above five steps are completed, the onReceive function will get a callback when Android system or Application issues a broadcast.

For example, open a file called NetChangeReceiver.java, as shown below. This example defines a broadcast receiver that listens for network changes. In addition, the registerreceiverby (context context context) and unregisterreceiverby (context context context) functions are customized to register broadcast receivers. The reason why register receiver and unregister receiver are encapsulated is to improve code manageability, code readability, code neatness, reduce debugging difficulty and code coupling.

package com.ocean.android.systembroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class NetChangeReceiver extends BroadcastReceiver {

    private static final String TAG = "NetChangeReceiver";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //throw new UnsupportedOperationException("Not yet implemented");
        Log.i(TAG, "onReceive: in");
        //add your code here
    }
    public void registerReceiverBy(Context context){
        Log.i(TAG, "registerReceiver: in");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        context.registerReceiver(this, intentFilter);
    }
    public void unregisterReceiverBy(Context context){
        Log.i(TAG, "unregisterReceiver: in");
        context.unregisterReceiver(this);
    }
}

The general steps for listening to the static registration method of Android system or Application broadcast are as follows:

  1. Define a XXXReceiver class that inherits BroadcastReceiver
  2. Override the onReceive(Context context, Intent intent) method of its parent class in the XXXReceiver class
  3. Added in the Android mainfest.xml file
<receiver
   android:name="com.ocean.android.systembroadcastreceiver.NetChangeReceiver "
   ...
   >
   <intent-filter>
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
   </intent-filter>
</receiver>

Application can broadcast by calling sendBroadCast() or sendOrderedBroadCast() of Context.

Matters needing attention

  1. It should be noted not to perform time-consuming operations in the onReceive function
  2. Android O (Android 8) has improved the BroadcastReceiver. The Intent to start the BroadcastReceiver must be the displayed Intent: either set the class name of XXXReceiver directly, or set the Intent through Action and package. And the start-up BroadcastReceiver is not allowed to start the Service in the background.
Published 6 original articles, won praise 1, visited 166
Private letter follow

Tags: Android Java network xml

Posted on Thu, 16 Jan 2020 11:54:46 -0500 by DavidT