一般的android应用都会监听手机网络变化,根据网络是否连接或者网络环境进行相应的操作或者限制。这里写下自己的实现方式。
效果.png
步骤:
- 自定义接收网络变化的广播接收器,接收到网络变化时通过EventBus发出通知
- 在所有activity的基类BaseActivity中注册自定义的广播接收器
- 在BaseActivity中接收EventBus的网络变化通知,来进行对话框形式的提示信息
1.自定义接收网络变化的广播接收器,在配置文件中添加权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
/**
* Created by dingmouren on 2018/8/1.
* 监听网络状态变更的广播接收器
*/
public class NetworkConnectChangedReceiver extends BroadcastReceiver{
private static final String TAG = "NetworkChangedReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == ConnectivityManager.CONNECTIVITY_ACTION) {
/*判断当前网络时候可用以及网络类型*/
boolean isConnected = NetworkUtils.isConnected();
NetworkUtils.NetworkType networkType = NetworkUtils.getNetworkType();
EventBus.getDefault().post(new NetworkChangeEvent(isConnected, networkType));
}
}
}
2.BaseActivity注册网络状态变化的广播接收器,并实现相应的UI提示。每一个activity可见的时候,这里都会在onResume中去检查网络是否连接,再者就是每当监听网络变化的广播接收器发出通知时,处理显示或者隐藏提示对话框。
/**
* Created by dingmouren on 2018/3/19.
* emial: naildingmouren@gmail.com
*/
public abstract class BaseActivity extends AppCompatActivity{
private static final String TAG = "BaseActivity";
protected Context mContext;
protected boolean mCheckNetwork = true;/*默认检查网络状态*/
protected boolean mNetConnected;/*网络连接的状态,true表示有网络,flase表示无网络连接*/
private NetworkConnectChangedReceiver mNetWorkChangReceiver;/*网络状态变化的广播接收器*/
private NetStateChangedDialog mNetStateChangedDialog;/*网络状态变化的提示对话框*/
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
EventBus.getDefault().register(this);
mNetStateChangedDialog = new NetStateChangedDialog(this);
//注册网络状态监听广播
mNetWorkChangReceiver = new NetworkConnectChangedReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetWorkChangReceiver, filter);
}
@Override
protected void onResume() {
super.onResume();
netStateChangedUI(NetworkUtils.isConnected());
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mNetWorkChangReceiver);
EventBus.getDefault().unregister(this);
}
/**
* 网络状态发生变化时的处理
* @param event
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onNetworkChangeEvent(NetworkChangeEvent event){
Log.i(TAG,"网络发生变化:"+event.toString());
mNetConnected = event.isConnected();
netStateChangedUI(event.isConnected());
}
/**
* 根据网络状态显示或者隐藏提示对话框
* @param isConnected
*/
private void netStateChangedUI(boolean isConnected){
if (mCheckNetwork) {
if (isConnected) {
if (null != mNetStateChangedDialog)mNetStateChangedDialog.dismiss();
}else {
if (null != mNetStateChangedDialog)mNetStateChangedDialog.show();
}
}
}
/**
* 设置是否要检查网络状态变化
* @param checkNetWork
*/
public void setCheckNetWork(boolean checkNetWork) {
mCheckNetwork = checkNetWork;
}
}
自定义对话框的实现:
NetStateChangedDialog.java
/**
* Created by dingmouren on 2018/8/2.
*/
public class NetStateChangedDialog extends Dialog {
public NetStateChangedDialog(@NonNull Context context) {
super(context, R.style.StyleNetChangedDialog);
View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_network_state_tip,null);
initView(contentView);
setContentView(contentView);
setCanceledOnTouchOutside(false);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.gravity = Gravity.TOP;
layoutParams.windowAnimations = R.style.StyleNetChangedDialog_Animation;
getWindow().setAttributes(layoutParams);
getWindow().setDimAmount(0f);/*使用时设置窗口后面的暗淡量*/
}
private void initView(View contentView) {
}
}
style.xml中的样式
<style name="StyleNetChangedDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="StyleNetChangedDialog.Animation" parent="Animation.AppCompat.Dialog">
<item name="android:windowEnterAnimation">@anim/translate_netstate_dialog_in</item>
<item name="android:windowExitAnimation">@anim/translate_netstate_dialog_out</item>
</style>
anim的位移动画:
translate_netstate_dialog_in.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="-100%"
android:toXDelta="0"
android:toYDelta="0">
</translate>
translate_netstate_dialog_out.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="-100%">
</translate>
网友评论