Learning of broadcast broadcasting mechanism

Broadcast mechanism: standard broadcast, ordered broadcast

Standard broadcast: send a broadcast, all broadcast receivers will receive this broadcast

Sequential broadcast: Send a broadcast, and the broadcast receivers receive the broadcast in descending order according to the priority. When the broadcast receiver with the highest priority intercepts the broadcast being broadcast, the following receivers will not receive the information.

Broadcast receiver : You can freely register the broadcast you are interested in (when a corresponding broadcast is sent, the broadcast receiver can receive the broadcast and process it internally)

How to register broadcast:

1. Dynamic registration (register in code)

public class MainActivity extends AppCompatActivity{
    private IntentFilter intentFilter;
    private NetworkChangeReceiver receiver;
    public void onCreate(Bundle saveInstanceState){
          super.onCreate(saveInstanceState);
          setContentView(R.layout.main);
          // Intent filter, used to add the broadcast we want to listen to 
           intentFilter = new IntentFilter();
           intentFilter.addAction( "Add us a broadcast here" );
           receiver = new NetworkChangeReceiver();
           registerReceiver(receiver, intentFilter);//Register the broadcast to our defined broadcast receiver
          
    }  

/**
**Define a broadcast receiver and implement the onReceive(Context context, Intent intent) method
*/

    class NetworkReceiver extends BroadcastReceiver{

            public void onReceive(Context context,Intent intent){

                // After receiving the broadcast from the system, perform a series of internal operations     
           }
    }
}

 

2. Static registration (registered in the AndroidManifest.xml file)

Create a BroadcastReceiver through Android studio and implement the onReceive() method

In the AndroidManifiest.xml file, we need to register the broadcast receiver we defined

<reveiver
    android:name=".MyBroadCastReceiver"
    android:enabled="true"
    android:exported="true">

<!--添加intent-filter-->
    <intent-filter>
           <action android:name="my.example.broadcast"
    </intent-filter>
</receiver>

********************Note: Do not add too much logic or time-consuming operations in the onReceive() method**************** *********

After the above registration, when the receiver receives a broadcast of my.example.broadcast, the system will execute the method in onReceive()

For example: we send a broadcast by clicking the button

public class MainActivity extends BaseActivity{
    ....
......
    public void onCreate(Bundle saveInstance){
            ....
           Button button  =  (Button)findViewById(R.id.bt);
            button.setOnclickListener(new OnClickListener){
                   public void onClick(View v){
                      Intent intent = new Intent("my.example.broadcast" ); 
sendBroadcast(intent);///When changed to sendOrderedBroadcast(intent,null), an ordered broadcast will be sent

} } } }

The above code: When the button is clicked, the broadcast receiver is triggered

Ordered broadcasting and unordered broadcasting are global, although ordered broadcasting can set the weight of the broadcast we want to listen to by setting the android:priority="" of the intent-filter in each application, so that in onReceive() The broadcast transmission is interrupted by the abortBroadcast() method, but there are still security issues, which can be avoided by local broadcasting .

Local broadcast: mainly use LocalBroadcastManager to manage broadcast

public class LocalBroacastActivity extends AppCompatActivity {
    private Button bt;
    private IntentFilter intentFilter;
    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        bt = (Button)findViewById(R.id.button);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcasttest.LOCAL");
                localBroadcastManager.sendBroadcast (intent);
            }
        });
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcasttest.LOCAL");
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver (localReceiver, intentFilter);
    }

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

    class LocalReceiver extends BroadcastReceiver{

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

Implement forced offline function

 

Guess you like

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