广播监听网络
程序员文章站
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);
}
}
上一篇: python3版阿里云身份证识别
下一篇: 二维码生成扫描