import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
/**
* 网络相关工具类
*
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
*/
public class NetUtils {
/**
* 判断是否有网络连接
* <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
/**
* 判断WIFI网络是否可用
*/
public static boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
// return mWiFiNetworkInfo.isAvailable();
return mWiFiNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
}
}
return false;
}
/**
* 判断MOBILE网络是否可用
*/
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
// return mMobileNetworkInfo.isAvailable();
return mMobileNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
}
}
return false;
}
/**
* 获取当前网络连接的类型信息
*
* @return one of {@link ConnectivityManager#TYPE_MOBILE},{@link ConnectivityManager#TYPE_WIFI},
* {@link ConnectivityManager#TYPE_WIMAX},{@link ConnectivityManager#TYPE_ETHERNET},
* {@link ConnectivityManager#TYPE_BLUETOOTH},or other types defined by {@link ConnectivityManager}
*/
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
/**
* 获取IP地址 ipv4 ipv6
*/
public static String getIpAddress(boolean ipv4) {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress()) {
String sAddr = addr.getHostAddress();
boolean isIPv4 = sAddr.indexOf(':') < 0;
if (ipv4) {
if (isIPv4) {
return sAddr;
}
} else {
if (!isIPv4) {
// drop ip6 zone suffix
int delim = sAddr.indexOf('%');
return delim < 0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
}
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
}
网络相关工具类
最新推荐文章于 2024-03-16 09:41:08 发布