Broadcasting of the four major components of Android - basics

1. Dynamic registration

Create an internal class in the activity and inherit BroadcastReceiver (create a broadcast receiver), and
implement the logic after receiving the broadcast in the method onReceive (Context context, Intent intent).
Listen and register the broadcasts you need to receive in the onCreate method of the activity, and
cancel the broadcasts in onDestroy
. Example:

onCreate(){
	IntentFilter intentFilter - new IntentFilter();
	intentFilter.addActicon("广播名");
	广播类 广播对象 = new 广播构造函数();
	registerReceiver(广播对象,intentFilter);
}
onDestroy(){
	super.onDestroy();
	unregisterReceiver(广播对象);
}

Note:
1. Some permissions may encounter privacy issues. In this case, the permissions need to be declared in the AndroidManifest.xml file.
2. Multi-threading is not allowed in the broadcast onReceive method, and do not perform time-consuming operations in it. If you use this method If the program runs for a long time without ending, the program will report an error.
3. The broadcast receiver is more about opening other components of the program, or starting a service.
4. Dynamically registering the broadcast must start the application before it can be monitored, because it is in the oncreate method of the activity.

2. Static registration (allowing the program to receive broadcasts even if it is not started)

Create a new broadcast, automatically register the broadcast in AndroidManifest.xml (it must be registered in this file, if the code editor does not automatically create it, create it yourself), listen to the
broadcast that needs to be received in AndroidManifest.xml,
and implement the broadcast in onReceive. The specific logic behind
the example:

AndroidManifest.xml
<receiver
	android:name=".广播类"
	android:enabled="true"//是否启用这个广播
	android:exported="true">//是否允许接受本程序以外的广播
	<intent-filter>
		<action android:name="广播名"/>
	</intent-filter>
</receiver>

发送标准广播
Intent intent = new Intent("广播名");
sendBroadcast(intent);

发送有序广播(可截断广播,按顺序传递广播)
sendOrderedBroadcast(intent,null);//第二个参数是一个与权限相关的字符串
//设定广播接收器的先后顺序
AndroidManifest.xml
<receiver
	android:name=".广播类"
	android:enabled="true"//是否启用这个广播
	android:exported="true">//是否允许接受本程序以外的广播
	<intent-filter android:priority="100">//设定优先级
		<action android:name="广播名"/>
	</intent-filter>
</receiver>

//设定是否阶段广播的传播
在onReceive中调用abortBroadcast()方法表示截断这条广播

3. Local broadcast LocalBroadcastManager

The previous sending and receiving are all system global broadcasts, which can easily cause security problems. Example: Junk data was received and key data was intercepted.

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context:this);
localBroadcastManager.sendBroadcast(intent);//发送本地广播

//其他都不变,只有注册有变化  本地广播
localBroadcastManager.registerReceiver(广播对象,intentFilter);
//取消注册
localBroadcastManager.unregisterReceiver(广播对象);

Note: 1. Local broadcast cannot be received through static registration.
      2. Sending local broadcasts is more efficient than sending system global broadcasts.

Summarize

The more basic it is, the more important it is

Guess you like

Origin blog.csdn.net/qq_39734865/article/details/88790383