package com.bearbaby.erp.staff.util; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.widget.Toast; import com.bearbaby.erp.staff.MyApplication; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2017/4/19. */ public class NetUtils { private static boolean netStateLink = true; private static boolean registered = false;//防止重复注册 private static boolean showToasted = false; //防止application和启动页面重复初始化后,没网络提示语的重复 private static Context context; private static NetUtils instance; private static ConnectionChangeReceiver myNetReceiver; private static final String TAG = "NetUtils"; private NetUtils() { } public static synchronized NetUtils getInstance() { //对获取实例的方法进行同步 if (instance == null) instance = new NetUtils(); return instance; } //获得是否有网络连接 public static boolean isnetStateLink() { LogUtil.i(TAG, "isnetStateLink:是否有网络?"+netStateLink); return netStateLink; } /** * 这个一定要调用一次 * 否则应用无法联网一定要在网络请求之前调用 *启动页面初始化就好 为了安全起见,application一定要调用,startup也要调用, * 因为用户返回键退出后,不会再走application的创建,(应用退出,但是不杀死进程不会走application的) * @param contexts 一定要是Context,不能是Activity的this */ public static void init(Context contexts) { if (contexts != null) { context = contexts; ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) { netStateLink = false; if (!showToasted){ Toast.makeText(context,"联网失败,请检查网络设置",Toast.LENGTH_SHORT) .show(); showToasted=true; } LogUtil.i(TAG, "isnetStateLink: 初始化网络状态没有网络连接........."); } else { LogUtil.i(TAG, "isnetStateLink: 初始化网络状态有网络连接........."); netStateLink = true; } if (!registered){ registerReceiver();//注册....... } } } /** * 注册网络监听 */ private static void registerReceiver() { LogUtil.i(TAG, " 注册网络监听registerReceiver........."); IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); myNetReceiver = new ConnectionChangeReceiver(); context.registerReceiver(myNetReceiver, filter); registered=true; } /** * 取消网络注册监听 * 需要在应用最后一个页面销毁后注销(MainActivity退出后一定要调用), * 不然提示第一次进入后的联网失败,请检查网络设置,会重复 */ public static void unregisterReceiver() { LogUtil.i(TAG, " 反注册网络监听unregisterReceiver........."); if (registered){ try { if (context!=null){ context.unregisterReceiver(myNetReceiver); LogUtil.i(TAG, " 成功反注册网络监听........."); registered=false; } } catch (Exception e) { e.printStackTrace(); LogUtil.e(TAG, "异常反注册网络监听........."+e.getMessage()); registered=false; } registered=false; } showToasted=false; } static List<NetworkState> ln = new ArrayList(); //网络状态监听 static class ConnectionChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) { if (netStateLink) { if ( MyApplication.getContext().isAppVisible()) { ToastUtil.show("网络已断开!"); for (NetworkState ns : ln) { if (ns != null) { ns.disconnectNetwork(); } } } } netStateLink = false; } else { if (!netStateLink){ if ( MyApplication.getContext().isAppVisible()) { ToastUtil.show("网络已恢复!"); //通知监听,如果不在前台不通知,防止有网络后,做其他网络操作退出回到登录页面 netStateLink = true;//这个必须要在回调前修改为true 因为接口后面可能会有网络操作,到时候任然无法请求 for (NetworkState ns : ln) { if (ns != null) { ns.connectedNetwork(); } } } } netStateLink = true; } } } //添加网络监听和removeNetworkListen搭配 public void addNetworkListen(NetworkState ns) { if (ns != null) { ln.add(ns); } } //网络状态监听回调接口 public interface NetworkState { //连接上网络 void connectedNetwork(); //断开网络 void disconnectNetwork(); } //移除网络监听,和addNetworkListen搭配使用 public synchronized void en(NetworkState ns) { if (ns != null) { for (int i = 0; i < ln.size(); i++) { if (ln.get(i) == ns) { ln.remove(i); } } } } }