Broadcasting is a message mechanism. There are senders and receivers of messages. Broadcasting can realize inter-component communication and inter-process communication.
broadcast transmission
Global Broadcast Classification
1. Send a normal broadcast
Intent intent=new Intent("123");
sendBroadcast (intent);
2. Send an ordered broadcast:
Ordered broadcast is: broadcast receivers receive broadcasts in sequence according to their own priorities
priority:
<receiver android:name="ddd">
<intent-filter android:priority="100"/>
</receiver>
The priority range is: Integer.MIN~Integer.MAX, the priority range of the IntentFIlter of the system registered Receiver is -1000~1000. When the priority is the same, code-registered receivers take precedence over statically-registered receivers, and broadcast receivers installed first are higher than those installed later.
setResult: The function passes the result to the next receiver, and the getResult function obtains the result returned by the last BroadcastReceiver,
abort: The function to let the system discard the broadcast and use the broadcast to no longer transmit to other BroadcastReceivers.
Intent intent=new Intent("123");
sendOrderedBroadcast (intent, null);
3. Send sticky broadcast (deprecated in android 5.0/api 21, no longer recommended)
Sticky broadcast: This broadcast will exist until someone Context.removeStickyBroadcast removes it. The application that sends this broadcast must apply for the BROADCAST_STICKY permission.
Intent intent=new Intent("123");
sendStickyBroadcast(intent);
permission to broadcast
(Try not to send it to a third party, and restrict others from receiving it) The sender defines the authority, and the receiver applies for the authority.
sender
<permission android:name = "com.android.permission.RECV_XXX"/>
sendBroadcast(intent,"com.android.permission.RECV_XXX");
接收者
<uses-permission android:name="com.android.permission.RECV_XXX"></uses-permission>
(尽量不接第三方,限制别人发给我)接收者定义权限,发出者申请权限
接收者
<permission android:name = "com.android.permission.SEND_XXX"/>
<receiver android:name=".XXXReceiver"
android:permission="com.android.permission.SEND_XXX">
</receiver>
发出者
<uses-permission android:name="com.android.permission.SEND_XXX"></uses-permission>
sendBroadcaset(intent)
广播的标志位
intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
从3.1开始广播默认带这个默认带这个标志位。是说,此广播不发给没有运行的应用
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
此广播发给没有运行的广播。
应用内广播
Intent intent = new Intent();
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
增强广播的安全性:
1.不发给第三方,发送广播时,指定特定广播接收器所在的包名,这样指定包中的接收器才会收到,具体是通过intent.setPackage(packageName)指定在。
2.不接受第三方,<receiver android:name="ddd" android:exported="false">将exported属性人为设置成false默认是true,不接收第三方匹配的广播。
3.让广播带上权限认证permission。
4.使用应用内广播机制,即安全又高效。
广播的接收
BroadcastReceiver
//定义 public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String name = intent.getStringExtra("name"); } } // 注册 // 静态注册 <receiver android:exported=["true" | "false"] android:permission="string" > </receiver> <receiver android:name=".MyBroadcastReceiver" > <intent-filter> <action android:name="BROADCAST_ACTION" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> // 动态注册 mBroadcastReceiver = new MyBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BROADCAST_ACTION); registerReceiver(mBroadcastReceiver, intentFilter); // 解注册 unregisterReceiver(mBroadcastReceiver);