Day12 BroadCastReceiver 广播接受者

介绍

分类

1、有序广播(也叫标准广播)
2、无需广播

生命周期

静态注册和动态注册的区别:假如说Activity是接受者:
动态注册:
(1)广播接收者会跟Activity的生命周期的结束而结束;
(2)自由的控制注册和取消,有很大的灵活性
静态注册:
(1)广播接收者不会跟随Activity的生命周期的结束而结束,一直存在,即使应用程序关闭,也会被唤醒接受广播
(2)全局的广播

如何创建广播

静态创建

在清单文件注册广播接受者是静态的
在这里插入图片描述

动态创建

代码

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     sendId = findViewById(R.id.send_id);
     sendId.setOnClickListener(this);
     //1,创建一个广播接收者
     myReceiver = new MyReceiver();
     //添加广播过滤器
     IntentFilter intentFilter = new IntentFilter();
     //添加action
     intentFilter.addAction(BroadcastConst.ACTION);
     //注册
     registerReceiver(myReceiver,intentFilter);
 }

 @Override
 protected void onDestroy() {
     super.onDestroy();
     //注销广播
     unregisterReceiver(myReceiver);
 }
}

广播的发送

无序广播

代码

 send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //发送广播
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("name","哈哈哈");
                intent.putExtras(bundle);
                //设置频道号  必须在对应的清单文件里面设置你的频道
                intent.setAction("com.huang");
                sendBroadcast(intent);
            }
        });

清单文件里面的代码

 <receiver
            android:name=".MyReceiver2"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.huang" />
                <action android:name="com.huang3" />
                <!--<action android:name="com.huang3" />-->
            </intent-filter>
        </receiver>

有序广播

代码

 //发送有序广播
        send_Xu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //发送广播
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("name","有序广播3");
                intent.putExtras(bundle);
                //设置频道号  必须在对应的清单文件里面设置你的频道
                intent.setAction("com.huang3");
                //跟无序广播没有什么区别
                //两个参数 一个是Intent对象 一个是获取的权限 设定为null
                sendOrderedBroadcast(intent,null);
            }
        });

第一个广播接受者

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String name = extras.getString("name");
        Log.i(TAG, "onReceive: "+name);
    }
   

第二个广播接收者

public class MyReceiver2 extends BroadcastReceiver {
    private static final String TAG = "MyReceiver2";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String name = extras.getString("name");
        Log.i(TAG, "onReceive: "+name);
    }
}

清单文件中的代码

 <receiver
            android:name=".MyReceiver2"
            android:enabled="true"
            android:exported="true">
            //设置优先级
            <intent-filter android:priority="900">
                <action android:name="com.huang" />
                <action android:name="com.huang3" />
                <!--<action android:name="com.huang3" />-->
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <!--<action android:name="com.huang" />-->
                <action android:name="com.huang3" />
                <!--<action android:name="com.huang3" />-->
                <!--<action android:name="com.huang4" />-->
            </intent-filter>
        </receiver>

中断有序广播

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String name = extras.getString("name");
        Log.i(TAG, "onReceive: "+name);
        //判断是不是有序广播
        if(isOrderedBroadcast()){
            //中断一个广播
            abortBroadcast();
        }
    }
}

有序广播的注意事项

1、如果没有设置优先级的话,谁在上面谁就先接收
2、优先级的范围是-1000到1000
3、优先级越大,就越先接收广播
4、中断广播之后,后面的接受者就不能接收到消息了

系统广播

一些常用的系统广播网址
https://blog.csdn.net/cc_want/article/details/82344899

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值