1. SendOrderBroadcastActivity:
1 package com.example.broadcastdemo; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.content.IntentFilter; 6 import android.os.Bundle; 7 import android.view.View; 8 9 import androidx.annotation.Nullable; 10 11 public class SendOrderBroadcastActivity extends Activity { 12 13 private HighLevelReceiver mHighLevelReceiver; 14 private DefaultLevelReceiver mDefaultLevelReceiver; 15 private LowLevelReceiver mLowLevelReceiver; 16 17 @Override 18 protected void onCreate(@Nullable Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_send_order_broadcast); 21 22 registerBroadcastReceiver(); 23 } 24 25 /** 26 * 27 * @param view 28 */ 29 public void sendOrderBroadcast(View view){ 30 31 Intent intent = new Intent(); 32 intent.setAction(Constants.ACTION_ORDER_BROADCAST); 33 sendOrderedBroadcast(intent, null); 34 } 35 36 /** 37 * 注册动态广播 38 */ 39 public void registerBroadcastReceiver(){ 40 // 分别定义各自的意图过滤器 41 IntentFilter intentFilterHigh = new IntentFilter(); 42 IntentFilter intentFilterDefault = new IntentFilter(); 43 IntentFilter intentFilterLow = new IntentFilter(); 44 intentFilterHigh.addAction(Constants.ACTION_ORDER_BROADCAST); 45 intentFilterDefault.addAction(Constants.ACTION_ORDER_BROADCAST); 46 intentFilterLow.addAction(Constants.ACTION_ORDER_BROADCAST); 47 // 初始化各个广播接收者 48 mHighLevelReceiver = new HighLevelReceiver(); 49 mDefaultLevelReceiver = new DefaultLevelReceiver(); 50 mLowLevelReceiver = new LowLevelReceiver(); 51 // 设置有序等级,范围是-1000~1000,默认是0 52 intentFilterHigh.setPriority(1000); 53 intentFilterDefault.setPriority(0); 54 intentFilterLow.setPriority(-1000); 55 // 注册对应接收者的广播 56 this.registerReceiver(mHighLevelReceiver, intentFilterHigh); 57 this.registerReceiver(mDefaultLevelReceiver, intentFilterDefault); 58 this.registerReceiver(mLowLevelReceiver, intentFilterLow); 59 } 60 }
2. HighLevelReceiver(高级接收者):
1 package com.example.broadcastdemo; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.util.Log; 7 8 public class HighLevelReceiver extends BroadcastReceiver { 9 private static final String TAG = "HighLevelReceiver"; 10 11 @Override 12 public void onReceive(Context context, Intent intent) { 13 Log.d(TAG, "high action is -- " + intent.getAction()); 14 } 15 }
3. DefaultLevelReceiver(默认接收者,优先值默认为0):
1 package com.example.broadcastdemo; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.util.Log; 7 8 public class DefaultLevelReceiver extends BroadcastReceiver { 9 10 private static final String TAG = "DefaultLevelReceiver"; 11 12 @Override 13 public void onReceive(Context context, Intent intent) { 14 Log.d(TAG, "default action is -- " + intent.getAction()); 15 } 16 }
4. LowLevelReceiver:
1 package com.example.broadcastdemo; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.util.Log; 7 8 public class LowLevelReceiver extends BroadcastReceiver { 9 private static final String TAG = "LowLevelReceiver"; 10 11 @Override 12 public void onReceive(Context context, Intent intent) { 13 Log.d(TAG, "low action is -- " + intent.getAction()); 14 } 15 }
5. Constants(存放自定义的常量):
1 package com.example.broadcastdemo; 2 3 public class Constants { 4 // 自定义action的名字 5 public static final String ACTION_SEND_MSG = "com.example.broadcastdemo.SEND_MSG"; 6 public static final String ACTION_ORDER_BROADCAST = "com.example.broadcastdemo.ORDER_BROADCAST"; 7 public static final String KEY_CONTENT = "content"; 8 }
6. 输出结果如下:
com.example.broadcastdemo D/HighLevelReceiver: high action is -- com.example.broadcastdemo.ORDER_BROADCAST
com.example.broadcastdemo D/DefaultLevelReceiver: default action is -- com.example.broadcastdemo.ORDER_BROADCAST
com.example.broadcastdemo D/LowLevelReceiver: low action is -- com.example.broadcastdemo.ORDER_BROADCAST
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7. 终止向下传达与修改传达的内容并下发
在上文的基础上,对sendOrderBroadcast增加数据的传输:
1 /** 2 * 当按钮被按时,触发该函数以发送广播 3 * @param view 4 */ 5 public void sendOrderBroadcast(View view){ 6 7 Intent intent = new Intent(); 8 intent.setAction(Constants.ACTION_ORDER_BROADCAST); 9 // 通过bundle封装数据 10 Bundle bundle = new Bundle(); 11 bundle.putCharSequence("content", "我是被发送的广播内容..."); 12 /* 13 * @param intent The Intent to broadcast; all receivers matching this 14 * Intent will receive the broadcast. 15 * 16 * @param receiverPermission String naming a permissions that 17 * a receiver must hold in order to receive your broadcast. 18 * If null, no permission is required. 19 * 20 * @param resultReceiver Your own BroadcastReceiver to treat as the final 21 * receiver of the broadcast. 22 * 23 * @param scheduler A custom Handler with which to schedule the 24 * resultReceiver callback; if null it will be scheduled in the Context 's main thread. 25 * 26 * @param initialCode An initial value for the result code. Often 27 * Activity.RESULT_OK. 28 * 29 * @param initialData An initial value for the result data. Often 30 * null. 31 * 32 * @param initialExtras An initial value for the result extras. Often 33 * null. 34 * 有关使用bundle传值和intent的putExtra传值的区别:https://www.cnblogs.com/66it/p/10486029.html 35 */ 36 sendOrderedBroadcast(intent, null, null, null, Activity.RESULT_OK, null, bundle ); 37 38 }
接着,对HighLevelReceiver添加终止传达或者修改传达内容的代码:
1 package com.example.broadcastdemo; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.util.Log; 8 9 public class HighLevelReceiver extends BroadcastReceiver { 10 private static final String TAG = "HighLevelReceiver"; 11 12 @Override 13 public void onReceive(Context context, Intent intent) { 14 Log.d(TAG, "high action is -- " + intent.getAction()); 15 // 终止向下传达 16 // abortBroadcast(); 17 // 获取到传入的bundle 18 Bundle resultExtras = getResultExtras(true); 19 // 通过键值来获取内容 20 String content = resultExtras.getCharSequence("content").toString(); 21 Log.d(TAG, "content -- " + content); 22 // 修改广播内容 23 resultExtras.putCharSequence("content", "我是被high修改过的广播内容..."); 24 // 把修改过的bundle再传到下一个接收者中 25 setResultExtras(resultExtras); 26 } 27 }
对于其他的,也要定义变量来接收上一级接收者的广播(DefaultLevelReceiver或者LowLevelReceiver):
1 package com.example.broadcastdemo; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.util.Log; 8 9 public class DefaultLevelReceiver extends BroadcastReceiver { 10 11 private static final String TAG = "DefaultLevelReceiver"; 12 13 @Override 14 public void onReceive(Context context, Intent intent) { 15 Log.d(TAG, "default action is -- " + intent.getAction()); 16 Bundle resultExtras = getResultExtras(true); 17 String content = resultExtras.getCharSequence("content").toString(); 18 Log.d(TAG, "content -- " + content); 19 } 20 }
运行结果如下:
com.example.broadcastdemo D/HighLevelReceiver: high action is -- com.example.broadcastdemo.ORDER_BROADCAST
com.example.broadcastdemo D/HighLevelReceiver: content -- 我是被发送的广播内容...
com.example.broadcastdemo D/DefaultLevelReceiver: default action is -- com.example.broadcastdemo.ORDER_BROADCAST
com.example.broadcastdemo D/DefaultLevelReceiver: content -- 我是被high修改过的广播内容...
com.example.broadcastdemo D/LowLevelReceiver: low action is -- com.example.broadcastdemo.ORDER_BROADCAST
com.example.broadcastdemo D/LowLevelReceiver: content -- 我是被high修改过的广播内容...