BroadCast broadcast in android

I found a giant artificial intelligence learning website a few days ago, which is easy to understand and humorous. I can’t help but share it with everyone. Click to jump to the tutorial.
Broadcasting is divided into general broadcast and ordered broadcast. If the broadcast receiver registers, it is divided into static registration and dynamic registration.
In the implementation of writing code, generally write the broadcast receiving end first, and then write the sending broadcast end.

1. Static registration

For static registration, we only need to write an inherited BroadcastReceiver, and then configure it in the function manifest file.

 <receiver android:name="com.hbk.brocastreceivedemo.MyBrocastReceiver1">
    <intent-filter >
         <action android:name="com.hbk.brocastreceivedemo.MyBrocastReceiver1.action"/>
     </intent-filter>
 </receiver>

Broadcast transmission constructs an Intent object based on the name of the action.

Intent intent = new Intent("com.hbk.brocastreceivedemo.MyBrocastReceiver1.action");
intent.putExtra("author", "huangbaokang");
sendBroadcast(intent);

You can also carry data. When sending a broadcast, if you register statically, you will build the object every time (print the log in the constructor to verify), and then you can accept the corresponding parameters in the onReceive() method

	@Override
	public void onReceive(Context context, Intent intent) {
    
    
		String author = intent.getStringExtra("author");
		Log.i("TAG","MyBrocastReceiver1 onReceive()  "+ author );
	}

Two, dynamic registration

package com.hbk.brocastreceivedemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
    
    
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	private BroadcastReceiver receiver;
	// 注册广播接收器
	public void registBR(View v ){
    
    
		// 动态注册,不需要在功能清单文件中配置
		if(receiver == null){
    
    
			receiver = new MyBrocastReceiver2();
			IntentFilter filter = new IntentFilter("com.hbk.brocastreceivedemo.MyBrocastReceiver1.action");
			registerReceiver(receiver , filter );
			Toast.makeText(this, "动态注册", 0).show();
		}else{
    
    
			Toast.makeText(this, "已经注册了", 0).show();
		}
	}
	
	// 解注册广播接收器
	public void unRegistBR(View v){
    
    
		if(receiver !=null ){
    
    
			unregisterReceiver(receiver);
			receiver = null;
			Toast.makeText(this, "解注册广播接收器", 0).show();
		} else {
    
    
			Toast.makeText(this, "还没有注册广播接收器", 0).show();
		}
	}
	
	@Override
	protected void onDestroy() {
    
    
		super.onDestroy();
		if(receiver!=null) {
    
    
			unregisterReceiver(receiver);
			receiver = null;
		}
	}
	
}

Use the code method to pass in IntentFilter. Note that after dynamic registration, if you press the exit key, an error will be reported, prompting that you need to call unregisterReceiver, and we can override the onDestroy method. During dynamic registration, the constructor will be executed once, and during subsequent registration, the constructor will not be executed. This is a difference from static registration. And dynamic registration does not need to be configured in the manifest file.

Three, orderly broadcasting

Ordered broadcasting is different from general broadcasting. In general broadcasting, if there are 10 receiving ends, one broadcast will be sent, and 10 receiving ends will receive it at the same time, and there is no order. In orderly broadcasting, there is a certain receiving order, and it can be decided whether to interrupt the broadcasting. If it is interrupted, the subsequent receiving end will not receive it.
api introduction
sendOrderedBroadcast(Intent intent, String receiverPermission)
We need to configure it in the function configuration list just like general broadcasting, but ordered broadcasting has a priority configuration, and the name of the action must be the same as the one that sends the broadcast.

// 发送有序广播
	public void sendOrderBC(View v){
    
    
		Intent  intent = new Intent("com.hbk.brocastreceivedemo.lover.action");
		intent.putExtra("lover", "zhanglulu");
		sendOrderedBroadcast(intent , null);
	}

Insert picture description here
Because MyBrocastReceiver4 has a higher priority, the message is received first
Insert picture description here

If you add a code that interrupts the broadcast in MyBrocastReceiver4.java and run it again, MyBrocastReceiver3 will not receive the broadcast.
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/huangbaokang/article/details/112344864