Android获取设备信息(包括联网状态wifi/4G/3G/2G)

最近又涉及到了获取设备信息的东西,因为之前做过但是没有做记录,导致这次又做的时候又重新去百度,所以这次做完了要记录一下。

1.获取手机厂商

public String getDeviceBrand(){
    return android.os.Build.BRAND;
}

2.获取设备型号

public String getSystemModel() {
    return android.os.Build.MODEL;
}

3.获取当前系统版本

public String getSystemVersion() {
    return android.os.Build.VERSION.RELEASE;
}

4.获取运营商的名字

 public static String getOperatorName(Context context) {
    /*
     * getSimOperatorName()就可以直接获取到运营商的名字
     * 也可以使用IMSI获取,getSimOperator(),然后根据返回值判断,例如"46000"为移动
     * IMSI相关链接:http://baike.baidu.com/item/imsi
     */
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    // getSimOperatorName就可以直接获取到运营商的名字
    return telephonyManager.getSimOperatorName();
}

5.获取联网方式

public static String getNetworkState(Context context) {
    String strNetworkType = "";
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // 获取网络服务
    //NetworkInfo networkInfo = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
    NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isAvailable())
    {
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
        {
            strNetworkType = "WIFI";
        }
        else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
        {
            String _strSubTypeName = networkInfo.getSubtypeName();

            // 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;
            }
        }
    }
    return strNetworkType;
}

6.获取屏幕分辨率

public static Integer[] getScreenWH(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay()
            .getMetrics(dm);

    return new Integer[]{dm.widthPixels, dm.heightPixels};
}

7.检查当前网络是否可用

public static boolean isNetworkAvailable(Context context) {
    /* 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) */
    if (null == context) {
        context = MyApplication.getInstance();
    }
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager == null) {
        return false;
    } else {
        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

        if (networkInfo != null && networkInfo.length > 0) {
            for (int i = 0; i < networkInfo.length; i++) {
                /* 判断当前网络状态是否为连接状态 */
                if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

你可能感兴趣的:(Android获取设备信息(包括联网状态wifi/4G/3G/2G))