Comparison and induction of several ways of broadcasting registration

Some time ago, I just saw the broadcasting chapters of "First Line Code". In order to facilitate understanding, I collate and summarize the codes in the book, and compare the different ways of registering broadcasting receivers. The codes in the article are all taken from "First Line Code". As I am a novice, if there is any wrong correction, I would welcome it.

1. Classification

Ordered broadcasting: asynchronous execution with all receivers receiving simultaneously (sendbroadcast (intent))

Standard broadcasting: synchronous execution, sequential reception (sendOrdered Broadcast (intent, null))

 

2 Overview of registration process

This paper accepts the broadcasting registration process framework, and then gives the implementation code of the specific steps. There are two main ways of broadcasting registration: static and dynamic.

1 Static: Added <intent-filter> to Android Mannifest.xml, commonly used with system-owned broadcasting

2 Dynamics: It is mainly added dynamically in java code, which is relatively flexible. Customization and local broadcasting are registered dynamically, mainly through intent.

Overview of process

(1) Static

  • Custom Broadcast Receiver, Inheriting Broadcast
  • Add <intent-filter>to Android Mannifest.xml

(2) Dynamics

  • Custom Broadcast Receiver, Inheriting Broadcast
  • Set up Intentfilter and register Receiver (intentFilter);
  • unregisterReceiver(netWorkChangeReceiver);
  • Add uses-permission, such as <uses-permission and roid: name="android.permission.ACCESS_NETWORK_STATE"/>

(3) Custom Broadcasting

  • Custom Broadcast Receiver, Inheriting Broadcast
  • Add INTENT-FILTER to Android Mannifest.xml (name taken by oneself, such as <action android:name="MyBradcastReceiver"/>, equivalent to setting Intentfilter)
  • Send Broadcast (Standard Broadcast is sedbroadcast, Ordered Broadcast is sendOrdered Broadcast)

(4) Local broadcasting

  • Custom Broadcast Receiver, Inheriting Broadcast
  • Define required variables (Local Broadcast Manager, IntentFilter, Local Receiver custom broadcasting (broadcasting receiver)
  • Send Broadcast (intent);
  • Set the filter intentFilter = new IntentFilter("com.example.anotherbroadcast.LOCAL_BROADCAST");
  • Register the local Broadcast Manager. registerReceiver (local Receiver, intentFilter);
  • unregisterReceiver(netWorkChangeReceiver);
                    
                   
                   

 

                                   

3. Detailed registration method

1) Dynamic registration

(1) Setting up intentfilter and registering monitor

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//Intra-system broadcasting, which responds to changes in the network
        netWorkChangeReceiver=new NetWorkChangeReceiver() ;
       registerReceiver(netWorkChangeReceiver,intentFilter) ;

(2) Setting up broadcast receiving class, inheriting Broadcast

class NetWorkChangeReceiver extends BroadcastReceiver{
 @Override
        public void onReceive(Context context, Intent intent) {
            //Add the desired functionality

        }
    }

(3) At the same time, don't forget to cancel the registration, usually in the activity's onDestory.

 @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(netWorkChangeReceiver);
    }

And don't forget to add Android Mannifest. XML for network status broadcasting

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />2)Static registration

Take boot-up broadcasting as an example. AndroidMannifest.xml Add the following code. BootCompleteReceiver Broadcasting for self-writing, inheritance Broadcast

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
    android:name=".BootCompleteReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

3)Custom Broadcasting

1)Custom Broadcasting Receiver

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"received in mybroadreceiver",Toast.LENGTH_SHORT).show();
      
    }
}

2) Add INTENT-FILTER to Android Mannifest.xml.

<receiver android:name=".MyBroadcastReceiver">
            <intent-filter >
                <action android:name="MyBradcastReceiver"/>
            </intent-filter>
        </receiver>

3) sending broadcasting (standard broadcasting is sedbroadcast, orderly broadcasting is sendOrdered Broadcast)

 Intent intent = new Intent() ;
                intent.setAction("MyBradcastReceiver") ;
                intent.setComponent(new ComponentName("com.example.anotherbroadcast"
                                    ,"com.example.anotherbroadcast.AnotherBroadcast"));//The first parameter is the packet where the receiver is located, and the second is the receiver class.
                sendBroadcast(intent);//Standard Broadcasting
                sendOrderedBroadcast(intent,null);//Orderly broadcasting

4) Local Broadcasting

(1) Custom Broadcasting Receiver

 class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"received local broadcast",Toast.LENGTH_SHORT).show() ;
        }
    }

(2) Define the required variables

  LocalBroadcastManager localBroadcastManager ;//Transmitting broadcasting needs
     IntentFilter intentFilter ;//Select the desired broadcast (broadcast filter)
     LocalReceiver localReceiver ;//Custom Broadcasting (Broadcasting Receiver)

(3) Core code (the filters here are relatively flexible and do not need to be added to Android Mannifest. xml)

                //Send Broadcasting
Intent intent = new Intent("com.example.anotherbroadcast.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(intent);//Send local broadcasts
                //catalog filter
                intentFilter = new IntentFilter() ;
                intentFilter.addAction("com.example.anotherbroadcast.LOCAL_BROADCAST");
                //Registered Broadcasting
                localReceiver = new LocalReceiver() ;
                localBroadcastManager.registerReceiver(localReceiver,intentFilter);//Register Local Broadcasting Monitor

(4) Cancellation of registration

 @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);
    }

 

Keywords: Android xml network Java

Added by stefharley on Sun, 28 Jul 2019 08:36:19 +0300