网络判断utils

来源:互联网 发布:复合材料刚度矩阵 编辑:程序博客网 时间:2024/06/03 16:47
首先 清单文件加权限:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>public class NetWorkHelper {/** * 判断是否有网络连接 */public static boolean isNetworkAvailable(Context context) {ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity == null) {Toast.makeText(context, "couldn't get connectivity manager", 1).show();} else {NetworkInfo[] info = connectivity.getAllNetworkInfo();if (info != null) {for (int i = 0; i < info.length; i++) {if (info[i].isAvailable()) {Toast.makeText(context, "network is available", 1).show();return true;}}}}Toast.makeText(context, "network is not available", 1).show();return false;}/** * 判断网络是否为漫游 */public static boolean isNetworkRoaming(Context context) {ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity == null) {Toast.makeText(context, "couldn't get connectivity manager", 1).show();NetworkInfo info = connectivity.getActiveNetworkInfo();if (info != null&& info.getType() == ConnectivityManager.TYPE_MOBILE) {TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);if (tm != null && tm.isNetworkRoaming()) {Toast.makeText(context, "network is roaming", 1).show();return true;} else {Toast.makeText(context, "network is not roaming", 1).show();}} else {Toast.makeText(context, "not using mobile network", 1).show();}}return false;}/** * 判断MOBILE网络是否可用 *  * @param context * @return * @throws Exception */public static boolean isMobileDataEnable(Context context) throws Exception {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);boolean isMobileDataEnable = false;isMobileDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();Toast.makeText(context, "MOBILE网络 is "+isMobileDataEnable, 1).show();return isMobileDataEnable;}/** * 判断wifi 是否可用 * @param context * @return * @throws Exception */public static boolean isWifiDataEnable(Context context) throws Exception {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);boolean isWifiDataEnable = false;isWifiDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();Toast.makeText(context, "WIFI is "+isWifiDataEnable, 1).show();return isWifiDataEnable;}/** * 判断网络类型 * @param context * @return * @throws Exception */public static int getConnectedType(Context context) throws Exception {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();if(networkInfo!=null&&networkInfo.isAvailable()){Toast.makeText(context, "type is "+networkInfo.getType(), 1).show();return networkInfo.getType();}return -1;}}

0 0