BroadCastReceiver

本文深入解析Android中的广播机制,包括静态和动态注册的区别,以及在Android 8.0及更高版本中的变化。详细介绍了如何创建广播接收器,注册过程,以及发送有序和无序广播的方法。同时,通过实例演示了如何设置广播的优先级,以及如何在广播接收器中处理接收到的数据。
摘要由CSDN通过智能技术生成

接收

注册:分为两种,静态注册和动态注册

首先写一个类继承BroadCastReceiver

public class MyBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,intent.getStringExtra("name"),Toast.LENGTH_SHORT).show();

    }
}

静态注册:

AndroidManifest.xm 的 <application>中

<receiver android:name=".ui.broadcastreceiver.MyBroadCastReceiver">

    <intent-filter >

        <action android:name="com.example.sample"/>
    </intent-filter>
</receiver>

发现在android8.0当中静态广播接收不到,百度搜索一下有人说是8.0限制了静态注册

动态注册:

private MyBroadCastReceiver myBroadCastReceiver=new MyBroadCastReceiver();
   @Override
    protected void initView() {
        IntentFilter intentFilter=new IntentFilter("com.example.sample");
        registerReceiver(myBroadCastReceiver,intentFilter);
    }

com.example.sample与发送广播时的字符串对应即可

发送

广播发送分为有序还是无序

无序广播,所有接收者都可以接收

Intent intent=new Intent();
intent.setAction("com.example.sample");
intent.putExtra("name","啦啦啦");
sendBroadcast(intent);

有序广播,按顺序接收

优先级1000到-1000

既然之前说到8.0不支持静态注册,那么我直接这样写

private MyBroadCastReceiver myBroadCastReceiver=new MyBroadCastReceiver();
private MyBroadCastReceiver1 myBroadCastReceiver1=new MyBroadCastReceiver1();
private MyBroadCastReceiver2 myBroadCastReceiver2=new MyBroadCastReceiver2();
@Override
protected void initView() {

    IntentFilter intentFilter=new IntentFilter("com.example.sample");
    intentFilter.setPriority(1000);
    registerReceiver(myBroadCastReceiver,intentFilter);

    IntentFilter intentFilter1=new IntentFilter("com.example.sample");
    intentFilter.setPriority(0);
    registerReceiver(myBroadCastReceiver1,intentFilter1);

    IntentFilter intentFilter2=new IntentFilter("com.example.sample");
    intentFilter.setPriority(-1000);
    registerReceiver(myBroadCastReceiver2,intentFilter2);
}
@OnClick({R.id.no_order,R.id.order})
void sendBroad(View view){
    switch (view.getId()){
        case R.id.no_order:
            
            break;
        case R.id.order:
           Intent intent=new Intent(); intent.setAction("com.example.sample");    
           sendOrderedBroadcast(intent,null,null,null,0,"啦啦啦",null);
            break;
    }
}

以下是三个接收数据类:

public class MyBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("data==",getResultData());
        setResultData("修改数据");

    }
}
public class MyBroadCastReceiver1 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("data1==",getResultData());
        abortBroadcast();
    }
}
public class MyBroadCastReceiver2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("data2==",getResultData());

    }
}

打印结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值