安卓 广播在baseActivity中的封装

之前项目使用的是 广播来通知消息 的  代码大家都知道

这是一个简单的发送广播的代码片段
Intent intent = new Intent("net.jxx.action");
        intent.putExtra(“type”, type);
  sendBroadcast(intent);




这是一个简单的接受广播的的代码片段
IntentFilter intentFilter = new IntentFilter(
                "net.jxx.action");
     BroadcastReceiver   broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int type = intent.getIntExtra(“type”, -1);
                
            }
        };

registerReceiver(broadcastReceiver, intentFilter);
unregisterReceiver(broadcastReceiver, intentFilter);

可是每一次都得写这么多的代码 。代码量比较大。后来使用enevtbus  需要许多的javabean对象。后楼梯不胜其烦.这次在封装自己使用的mvp+retrofit+Rxjava+baseActivity(点击这里)在考虑广播的使用的时候,决定封装到baseActivity。为什么封装到baseActivity。baseActivity中有activity的生命周期。可以直接注册,注销。

但是问题出现了。比如说1个类 我需要好几参数,怎么办?我使用的是可变参数。解决不管是几个参数都需要注册的问题。那么这样的话 在子activity  就可以实现一句话发送广播和接受广播的问题。

同时为了具体区不同的状况 。决定使用  接口来绑定区分收到的不同参数


    /**
     发送的时候    只需要sendMyBroadcast(int  type)
     */
    public void sendMyBroadcast(int  type) {
        sendBroadcast(Constant.createIntent(
                type));

    }




    /**
     接受的时候    initReceiver
     */
    public void initReceiver(final BroadcastInterface myinterface, final int...  statue) {
        intentFilter = new IntentFilter(
                Constant.ACTION);
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int type = intent.getIntExtra(Constant.BROADCAST_KEY_type, Constant.TYPE_NONE);
                if (type== Constant.TYPE_NONE){
                    myinterface.receiverFailure();
                }else {
                    myinterface.receiverSuccess( intent,type);
                }
            }
        };

        registerReceiver(broadcastReceiver, intentFilter);
    }

 

这是常量类的参数

 

public class Constant {


    public static Intent createIntent(int type) {
        Intent intent = new Intent(ACTION);
        intent.putExtra(BROADCAST_KEY_type, type);
        return intent;
    }

    public static final String ACTION = "net.jxx.action";
    public static final String BROADCAST_KEY_type = "type";
    public static final int TYPE_NONE = -1;
}
这是绑定接口具体方法
public interface BroadcastInterface {
     void receiverSuccess(Intent intent, int statue);
     void receiverFailure();

}

子antivity中使用:

发送的时候
 sendMyBroadcast(1001);

子antivity中接受使用:

initReceiver(this,1001,1000);

同时注意  接受的activity实现接口 BroadcastInterface的方法

这样就可以在接口中进行判断了

 @Override
    public void receiverSuccess(Intent intent, int statue) {
        Log.e("jxx",statue+">>");
        Toast.makeText(MainActivity.this,statue+"   ",Toast.LENGTH_SHORT).show();
        content.setText("  "+statue);
    }

    @Override
    public void receiverFailure() {

    }
}

注意:广播的注册也需要unregisterReceiver,可以在  baseactivity中 的ondestory 中解绑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值