BroadCastReceiver运用

目录

全局广播

本地广播


全局广播

默认发送的广播,就是全局广播,所有的App都可以请求接收

全局广播可以动态注册,也可以静态注册

<Button
            android:text="静态广播"
            android:onClick="stareceiver"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></Button>
public void stareceiver(View view) {
        Intent intent = new Intent();
        intent.setAction("com.bw.sta");//设定动作
        Bundle bundle = new Bundle();
        bundle.putString("name","好好学");
        bundle.putInt("age",21);
        bundle.putFloat("money",22.2f);
        intent.putExtras(bundle);
        sendBroadcast(intent);//发送广播
    }
@Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.bw.sta")){
            Bundle bundle = intent.getExtras();
            String name = bundle.getString("name");
            int age = bundle.getInt("age");
            float money = bundle.getFloat("money");
            Toast.makeText(context,"静态广播"+name+age+money,Toast.LENGTH_SHORT).show();
        }
    }

动态广播

需要用到registerReceiver

<Button
            android:text="动态广播"
            android:onClick="dyneceiver"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></Button>
public void dyneceiver(View view) {
        Intent intent = new Intent();
        intent.setAction("com.bw.xx");
        Bundle bundle = new Bundle();
        bundle.putString("name","好好学");
        bundle.putInt("age",21);
        bundle.putFloat("money",22.4f);
        intent.putExtras(bundle);
        sendBroadcast(intent);
    }
@Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.bw.xx")){
            Bundle bundle = intent.getExtras();
            String name = bundle.getString("name");
            int age = bundle.getInt("age");
            float money = bundle.getFloat("money");
            Toast.makeText(context,"动态广播"+name+age+money,Toast.LENGTH_SHORT).show();
        }
    }
private OneReceiver oneReceiver;
    private TwoReceiver twoReceiver;
    private MyDynReceiver myDynReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDynReceiver = new MyDynReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.bw.xx");
        registerReceiver(myDynReceiver,intentFilter);
    }

本地广播

发送的广播事件不被其他运用程序获取,也不能响应其他运用程序发送的广播事件。

本地广播只能被动态注册,不能静态注册。动态注册或方法需要用到LocalBroadcastManager。

本地广播的优点:可以明确的知道正在发送的广播不会离开我们的程序,不用担心机密数据泄露,其他程序无法将广播发送到我们的程序内部,不需要担心安全问题,发送本地广播比系统全局效率高。

<Button
        android:onClick="Local"
        android:text="本地广播"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></Button>
public void Local(View view) {
        Intent intent = new Intent();
        intent.setAction("com.bw.local");
        localBroadcastManager.sendBroadcast(intent);
    }
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyLocalReceiver myLocalReceiver =new MyLocalReceiver();
        IntentFilter intentFilter =new IntentFilter();
        intentFilter.addAction("com.bw.local");
        localBroadcastManager=LocalBroadcastManager.getInstance(this);
        localBroadcastManager.registerReceiver(myLocalReceiver,intentFilter);
    }
@Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.bw.local")){
            Toast.makeText(context,"本地播放",Toast.LENGTH_SHORT).show();
        }
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值