private String getLocalIPAddress() throws SocketException{
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)){
return inetAddress.getHostAddress().toString();
}
}
}
return "null";
}
获取当前连接网络的类型
public static NetState getCurrentNetWorkState(){
ConnectivityManager mConnManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mInfo = mConnManager.getActiveNetworkInfo();
if(null == mInfo)
return NetState.NULL;
Log.e("ddd-------------", mInfo.getTypeName());
if (mInfo.isAvailable() && mInfo.isConnected()&& mInfo.getTypeName().equals("WIFI"))
return NetState.WIFI;
else if(mInfo.isAvailable() && mInfo.isConnected()&& mInfo.getTypeName().equals("ETHERNET"))// ETHERNET
return NetState.ETH;
return NetState.NULL;
}
public enum NetState{ NULL,WIFI,ETH }