欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

广播监听网络

程序员文章站 2022-07-15 14:59:42
...

MainActivity

//定义成员变量
 protected Context mContext;
    protected ACache mACache;
    protected boolean mCheckNetWork = true; //默认检查网络状态







 @Override
    protected void setNetWork() {
        super.setNetWork();
        NetState receiver = new NetState();
        IntentFilter filter = new IntentFilter();
        initTipView();
        mContext = this;
        //this.mACache = ACache.get(mContext);
        //MyApp.addActivity(this);
        EventBus.getDefault().register(this);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        this.registerReceiver(receiver, filter);
        receiver.onReceive(this, null);
    }











    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Success(SuccessEventBus successEventBus) {
        if (successEventBus.getSuccessNum() == 2) {
            Toast.makeText(this, "4G网络已连接", Toast.LENGTH_SHORT).show();
        }else if (successEventBus.getSuccessNum() == 3) {
            Toast.makeText(this, "wifi网络已连接", Toast.LENGTH_SHORT).show();
        }
        if (mTipView != null && mTipView.getParent() != null) {
            mWindowManager.removeView(mTipView);
        }

    }

 

相关标签: a'