public static final int CONNECT_NET_NONE = 0; public static final int CONNECT_NET_WIFI = 1; public static final int CONNECT_NET_MOBILE = 2; public static int getConnectNetType(Context context) { if (context == null) { return CONNECT_NET_NONE; } ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { return CONNECT_NET_NONE; } NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) { NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (networkInfo == null) { return CONNECT_NET_WIFI; } NetworkInfo.State state = networkInfo.getState(); if (state == NetworkInfo.State.CONNECTED) { return CONNECT_NET_MOBILE; } else { return CONNECT_NET_WIFI; } } return CONNECT_NET_NONE; }
2:判断当前是否联网
public static boolean isNetConnected(Context context) { if (context == null) { return false; } ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo info = connectivityManager.getActiveNetworkInfo(); return info != null && info.isAvailable(); } return false; }
3:获取移动网络下ip地址
public static String getMobileIp() { try { Enumerationen = NetworkInterface.getNetworkInterfaces(); if (en == null) { return ""; } for (en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface networkInterface = en.nextElement(); if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) { continue; } InetAddress preferredAddress = null; for (Enumeration enumeration = networkInterface.getInetAddresses(); enumeration.hasMoreElements(); ) { InetAddress inetAddress = enumeration.nextElement(); //skip ipv6 local address if (inetAddress.isLoopbackAddress() || inetAddress.islinkLocalAddress()) { continue; } //prefer ipv4 over ipv6 if (inetAddress instanceof Inet4Address) { preferredAddress = inetAddress; break; } //take first ipv6 address if (preferredAddress == null) { preferredAddress = inetAddress; } } if (preferredAddress != null) { return format(preferredAddress); } } } catch (Throwable e) { e.printStackTrace(); } return ""; } private static String format(InetAddress address) { if (address instanceof Inet4Address) { return address.getHostAddress(); } else if (address instanceof Inet6Address) { final String ip = address.getHostAddress(); final int index = ip.indexOf('%'); if (index >= 0) { return ip.substring(0, index); } else { return ip; } } return ""; }
4:获取wifi下ip
public static String getWifiIp(Context context) { final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null) { int ipAddress = wifiInfo.getIpAddress(); if (ipAddress != 0) { return intToIp(ipAddress); } } return ""; } private static String intToIp(int i) { return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF); }
5:获取ip地址
public static String getIp(Context context) { try { if (getConnectNetType(context) == CONNECT_NET_WIFI) { return getWifiIp(context); } else { return getMobileIp(); } } catch (Throwable e) { Log.e("xxxx", "getip :", e); } return ""; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)