测试引入1、ReceiverpublicclassExplicitAndImplicitReceiverextendsBroadcastReceiver{publicstaticfinalStringTAGExplicitAndImplicitReceiver.class.getSimpleName();publicstaticfinalStringACTIONbroadcast.ExplicitAndImplicitReceiver;OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,onReceive start);if(intentnull){Log.i(TAG,intent is null);return;}Stringactionintent.getAction();if(actionnull){Log.i(TAG,action is null);return;}if(!action.equals(ACTION)){Log.i(TAG,action is not ACTION);return;}Log.i(TAG,onReceive finish);}}2、Activityactivity_explicit_and_implicit_receiver_test.xml?xml version1.0 encodingutf-8?androidx.constraintlayout.widget.ConstraintLayoutxmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:idid/mainandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.ExplicitAndImplicitReceiverTestActivityButtonandroid:idid/btn_send_explicit_broadcastandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text发送显式广播app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent/Buttonandroid:idid/btn_send_implicit_broadcastandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text发送隐式广播app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/btn_send_explicit_broadcast//androidx.constraintlayout.widget.ConstraintLayoutExplicitAndImplicitReceiverTestActivity.javapublicclassExplicitAndImplicitReceiverTestActivityextendsAppCompatActivity{publicstaticfinalStringTAGExplicitAndImplicitReceiverTestActivity.class.getSimpleName();privateButtonbtnSendExplicitBroadcast;privateButtonbtnSendImplicitBroadcast;OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_explicit_and_implicit_receiver_test);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),(v,insets)-{InsetssystemBarsinsets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left,systemBars.top,systemBars.right,systemBars.bottom);returninsets;});btnSendExplicitBroadcastfindViewById(R.id.btn_send_explicit_broadcast);btnSendImplicitBroadcastfindViewById(R.id.btn_send_implicit_broadcast);btnSendExplicitBroadcast.setOnClickListener(v-{Log.i(TAG,发送显式广播);IntentintentnewIntent(this,ExplicitAndImplicitReceiver.class);sendBroadcast(intent);});btnSendImplicitBroadcast.setOnClickListener(v-{Log.i(TAG,发送隐式广播);IntentintentnewIntent(ExplicitAndImplicitReceiver.ACTION);sendBroadcast(intent);});}}3、Test1不声明 receiver发送显式广播输出结果发送显式广播发送隐式广播输出结果发送隐式广播2声明 receiver不声明 intent-filterreceiverandroid:name.mybroadcast.ExplicitAndImplicitReceiverandroid:exportedfalse/发送显式广播输出结果发送显式广播 onReceive start action is null发送隐式广播输出结果发送隐式广播3声明 receiver声明 intent-filter匹配的 action发送显式广播输出结果发送显式广播 onReceive start action is null发送隐式广播输出结果# 理想情况 发送隐式广播 onReceive start onReceive finish# 实际情况 发送隐式广播4声明 receiver声明 intent-filter不匹配的 action发送显式广播输出结果发送显式广播 onReceive start action is null发送隐式广播输出结果# 理想情况 发送隐式广播 onReceive start action is not broadcast.ExplicitAndImplicitReceiver# 实际情况 发送隐式广播测试结果分析1、小结测试Receiverintent-filter显式广播结果隐式广播结果原因1无无收不到收不到未注册 Receiver2有无收到收不到隐式广播在 Android 8.0 被限制3有有匹配的 action收到收不到隐式广播在 Android 8.0 被限制4有有不匹配的 action收到收不到隐式广播在 Android 8.0 被限制静态注册的 Receiver 使用显式广播可以不加 intent-filter加不加都可以收到Receiver 内可以不检查 action直接接收广播如果要检查 action区分多个 action显式广播的 Intent 中需要包含 action2、实践publicclassExplicitAndImplicitReceiverextendsBroadcastReceiver{publicstaticfinalStringTAGExplicitAndImplicitReceiver.class.getSimpleName();OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,onReceive start);...Log.i(TAG,onReceive finish);}}receiverandroid:name.mybroadcast.ExplicitAndImplicitReceiverandroid:exportedfalse/Log.i(TAG,发送显式广播);IntentintentnewIntent(this,ExplicitAndImplicitReceiver.class);sendBroadcast(intent);显式广播与隐式广播-显式广播隐式广播目标指定明确指定接收者类名不指定接收者通过 IntentFilter 匹配接收范围只能被指定的接收者接收可以被多个接收者接收安全系数较高只能被指定接收者接收较低可能被其他应用接收应用场景应用内组件通信系统广播、跨应用通信Android 8.0 对隐式广播的限制从 Android 8.0API 26开始静态注册的广播接收器无法接收大多数隐式广播除非是豁免的广播例如系统广播动态注册测试1、ReceiverpublicclassExplicitAndImplicitReceiverextendsBroadcastReceiver{publicstaticfinalStringTAGExplicitAndImplicitReceiver.class.getSimpleName();publicstaticfinalStringACTIONbroadcast.ExplicitAndImplicitReceiver;OverridepublicvoidonReceive(Contextcontext,Intentintent){Log.i(TAG,onReceive start);if(intentnull){Log.i(TAG,intent is null);return;}Stringactionintent.getAction();if(actionnull){Log.i(TAG,action is null);return;}if(!action.equals(ACTION)){Log.i(TAG,action is not ACTION);return;}Log.i(TAG,onReceive finish);}}2、Activityactivity_explicit_and_implicit_receiver_test.xml?xml version1.0 encodingutf-8?androidx.constraintlayout.widget.ConstraintLayoutxmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:idid/mainandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.ExplicitAndImplicitReceiverTestActivityButtonandroid:idid/btn_send_explicit_broadcastandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text发送显式广播app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent/Buttonandroid:idid/btn_send_implicit_broadcastandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text发送隐式广播app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/btn_send_explicit_broadcast//androidx.constraintlayout.widget.ConstraintLayoutExplicitAndImplicitReceiverTestActivity.javapublicclassExplicitAndImplicitReceiverTestActivityextendsAppCompatActivity{publicstaticfinalStringTAGExplicitAndImplicitReceiverTestActivity.class.getSimpleName();privateExplicitAndImplicitReceiverexplicitAndImplicitReceiver;privateButtonbtnSendExplicitBroadcast;privateButtonbtnSendImplicitBroadcast;OverrideprotectedvoidonStart(){super.onStart();explicitAndImplicitReceivernewExplicitAndImplicitReceiver();IntentFilterintentFilternewIntentFilter(ExplicitAndImplicitReceiver.ACTION);registerReceiver(explicitAndImplicitReceiver,intentFilter);}OverrideprotectedvoidonDestroy(){super.onDestroy();if(explicitAndImplicitReceiver!null)unregisterReceiver(explicitAndImplicitReceiver);}OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_explicit_and_implicit_receiver_test);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),(v,insets)-{InsetssystemBarsinsets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left,systemBars.top,systemBars.right,systemBars.bottom);returninsets;});btnSendExplicitBroadcastfindViewById(R.id.btn_send_explicit_broadcast);btnSendImplicitBroadcastfindViewById(R.id.btn_send_implicit_broadcast);btnSendExplicitBroadcast.setOnClickListener(v-{Log.i(TAG,发送显式广播);IntentintentnewIntent(this,ExplicitAndImplicitReceiver.class);sendBroadcast(intent);});btnSendImplicitBroadcast.setOnClickListener(v-{Log.i(TAG,发送隐式广播);IntentintentnewIntent(ExplicitAndImplicitReceiver.ACTION);sendBroadcast(intent);});}}3、Test1不指定 IntentFilterIntentFilterintentFilternewIntentFilter();发送显式广播输出结果发送显式广播发送隐式广播输出结果发送隐式广播2指定 IntentFilterIntentFilterintentFilternewIntentFilter(ExplicitAndImplicitReceiver.ACTION);发送显式广播输出结果发送显式广播发送隐式广播输出结果发送隐式广播 onReceive start onReceive finish小结动态注册的 Receiver 使用隐式广播指定 IntentFilterReceiver 内可以不检查 action直接接收广播一个 action 的情况