android 获取ip地址,获取2,3,4g,wifi状态等

1.获取ip地址
public String getLocalIpAddress() {
if(((ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo()==null)
{
return “无网络”;
}
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(“WifiPreference IpAddress”, ex.toString());
}
return “无网络”;
}

2.获取2,3,4g和wifi
public static String GetNetworkType()
{
String strNetworkType = “”;
ConnectivityManager connectivityManager=(ConnectivityManager)ConfigManager.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
strNetworkType = “WIFI”;
}
else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
{
String _strSubTypeName = networkInfo.getSubtypeName();

            Log.e("cocos2d-x", "Network getSubtypeName : " + _strSubTypeName);

            // TD-SCDMA   networkType is 17
            int networkType = networkInfo.getSubtype();
            switch (networkType) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
                    strNetworkType = "2G";
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
                    strNetworkType = "3G";
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                    strNetworkType = "4G";
                    break;
                default:
                    // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                    if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) 
                    {
                        strNetworkType = "3G";
                    }
                    else
                    {
                        strNetworkType = _strSubTypeName;
                    }

                    break;
             }

            Log.e("cocos2d-x", "Network getSubtype : " + Integer.valueOf(networkType).toString());
        }
    }

    Log.e("cocos2d-x", "Network Type : " + strNetworkType);

    return strNetworkType;
}

3.判断有,无网络。
public boolean isNetworkAvailable(Activity activity)
{
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null)
{
return false;
}

    NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();    
    if (mNetworkInfo != null) {    
        return mNetworkInfo.isAvailable();    
    } 
    else{
        return false;
    }

}

你可能感兴趣的:(Android)