• android广播接收器的无序广播和有序广播


    1.无序广播,就普通广播,没有先后顺序,几乎同时收到消息

    2.有序广播,可以设置接收器的顺序优先级,还可以对广播设置设置截断,修改
    通过设置abortBroadcast()进行截断,通过setResultExtras(bundle)向下游广播接收器传递额外的键值对信息或者setResultData(“”)直接传送字符串,下游广播通过getResultExtras方法接收信息,getResultData()方法接收字符串信息

    示例有序广播
    清单文件:
    有序广播的顺序通过清单文件里的android:priority属性的数字大小进行优先级设置:范围为(-1000到1000)数字越大,优先级越高,

       
    
    • 1

    可参考下图:
    在这里插入图片描述

     
            
                
                    
                
            
    
            
                
                    
                
            
            
                
                    
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    /**
     * 有序广播01
     */
    public class MyHaveBroadcastReceiver01 extends BroadcastReceiver {
    
        private static final String TAG = "MyHaveBroadcastReceiver01";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                Bundle extras = intent.getExtras();
                String data = "";
                if (extras != null) {
                    data = extras.getString("name");
                }
                Log.d(TAG, "MyHaveBroadcastReceiver01 onReceive"+data);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    /**
     * 有序广播02
     */
    public class MyHaveBroadcastReceiver02 extends BroadcastReceiver {
    
        private static final String TAG = "MyHaveBroadcastReceiver02";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                Bundle extras = intent.getExtras();
                String data = "";
                if (extras != null) {
                    data = extras.getString("name3");
    
                    /**
                     * 2.1有序广播可以通过setResultExtras向下游广播接收器传递数据
                     */
                    extras.putString("name2", "喊你速速到龙阳路集合");
                    setResultExtras(extras);
                    /**
                     * 2.2有序广播可以通过setResultData向下游广播接收器传递字符串
                     */
                    setResultData("2号口的信息");
                }
                Log.d(TAG, "MyHaveBroadcastReceiver02 onReceive" + data);
                /**
                 * 1.有序广播可以通过abortBroadcast();方法对广播进行截断
                 */
    //            abortBroadcast();
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    /**
     * 有序广播03
     */
    public class MyHaveBroadcastReceiver03 extends BroadcastReceiver {
    
        private static final String TAG = "MyHaveBroadcastReceiver03";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                Bundle extras = intent.getExtras();
                String data = "";
                if (extras != null) {
                    data = extras.getString("name");
                }
                Log.d(TAG, "MyHaveBroadcastReceiver03 onReceive" + data);
                /**
                 * 获取上游广播传递过来的数据
                 */
                //方法1
                Bundle bundle = getResultExtras(true);
                String strData = bundle.getString("name2");
                String resultData = getResultData();
                //方法2,直接获取字符串
                if (strData != null) {
                    Log.d(TAG, "上游广播传递来的信息 onReceive" + strData +"\t\t\t"+ resultData);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    Activity页面:StaticDynamicTestActivity.java

    /**
     * 静态广播和动态广播的测试
     * 默认情况下,静态注册的广播隐式的收不到
     * 有序广播和无序广播的测试
     */
    public class StaticDynamicTestActivity extends AppCompatActivity {
        Button btnHaveOrder;
        Button btnNoHaveOrder;
        private MyDyBroadcastReceiver myDyBroadcastReceiver = new MyDyBroadcastReceiver();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_static_dynamic_test);
    
            //注册动态广播接收器
            IntentFilter filter = new IntentFilter();
            filter.addAction("my_dy_broadcast_receiver");
            //注册
            registerReceiver(myDyBroadcastReceiver, filter);
            registerReceiver(mBatteryBroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    
            btnHaveOrder = findViewById(R.id.btn_haveOrder);
            btnNoHaveOrder = findViewById(R.id.btn_noOrder);
            haveOrder();
            noOrder();
        }
    
        //发送静态广播
        @SuppressWarnings("wrongConstant")
        public void sendStaticBroadcast(View view) {
            Intent intent = new Intent();
            intent.setAction("my_broadcast_receiver");
            //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
            //intent.addFlags(0x01000000);//不推荐
            intent.setPackage(getPackageName());
            sendBroadcast(intent);
        }
    
        //发送动态广播
        @SuppressWarnings("wrongConstant")
        public void sendDyBroadcast(View view) {
            Intent intent = new Intent();
            intent.setAction("my_dy_broadcast_receiver");
    //        //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
    //        //intent.addFlags(0x01000000);//不推荐
    //        intent.setPackage(getPackageName());
            sendBroadcast(intent);
        }
    
        BroadcastReceiver mBatteryBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int level = intent.getIntExtra("level", 0);
                Toast.makeText(context, "电池电量还有百分之" + level, Toast.LENGTH_LONG).show();
            }
        };
    
    
        /**
         * 有序广播
         */
        void haveOrder() {
            btnHaveOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setAction("my_have_broadcast_receiver");
    
                    //这里注意传递个bundle参数
                    Bundle bundle = new Bundle();
                    bundle.putString("name3","回家吃饭了");
                    intent.putExtras(bundle);
                    //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
                    intent.setPackage(getPackageName());
                    sendOrderedBroadcast(intent, null);
                }
            });
        }
    
        /**
         * 无序广播
         */
        void noOrder() {
            btnNoHaveOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setAction("my_no_broadcast_receiver");
                    //安卓8.0以及以后做了禁止后台运行,不加setPackage无法收到隐式广播或者以下
                    intent.setPackage(getPackageName());
                    sendBroadcast(intent);
                }
            });
        }
    
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //解除注册
            unregisterReceiver(myDyBroadcastReceiver);
            unregisterReceiver(mBatteryBroadcast);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    Activity对应的xml页面:activity_static_dynamic_test.xml

    
    
    
        
    
        
    
        
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    以上为简单使用参考:有问题加QQ:1393508286

  • 相关阅读:
    Scala011--Scala中的常用集合函数及操作Ⅱ
    整个表单设置disable禁用,单独某个功能设置不禁用
    vue2中的插槽
    主板损坏时间不自动更新,如何解决
    51-60==c++知识点
    CorelDRAW最新24.1.0.360版本更新介绍讲解
    数据聚合——DSL&RestAPI
    别再用Mybatis Plus 的伪批量新增了!
    react悬浮球效果展示
    Linux权限维持
  • 原文地址:https://blog.csdn.net/ShiXinXin_Harbour/article/details/127588212