** * Created by Michael on 2017/5/25. */ public class NetWorkUtils { /** * 判断网络是否连接 * * @param context * @return */ public static boolean isConnected(Context context) { if (null != context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (null != connectivityManager) { NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (null != networkInfo) { if (networkInfo.isConnected() && networkInfo.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } /** * 判断是否是wifi连接 */ public static boolean isWifi(Context context) { if (null != context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { return false; } NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (null != networkInfo) { return networkInfo.getType() == ConnectivityManager.TYPE_WIFI; } } return false; } /** * 判断是否是2G/3G/4G连接 */ public static boolean is4G(Context context) { if (null != context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { return false; } NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (null != networkInfo) { return networkInfo.getType() == ConnectivityManager.TYPE_MOBILE; } } return false; } /** * 打开网络设置界面 */ public static void openSettingNetwork(Activity activity) { Intent intent = new Intent("/"); ComponentName componentName = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(componentName); intent.setAction(Intent.ACTION_VIEW); activity.startActivityForResult(intent, 0); } }
网络工具类封装
最新推荐文章于 2022-04-13 05:00:17 发布