Skip to content

Latest commit

 

History

History
141 lines (135 loc) · 4.9 KB

about_network.md

File metadata and controls

141 lines (135 loc) · 4.9 KB

网络相关

打开网络设置界面

/**
 * 打开网络设置界面
 * 3.0以下打开设置界面
 */
public static void openWirelessSettings(Context context) {
    if (android.os.Build.VERSION.SDK_INT > 10) {
        context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
    } else {
        context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }
}

判断是否网络连接

/**
 * 判断是否网络连接
 * 需添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 */
public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Activity.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info != null && info.isConnected();
}

判断wifi是否连接状态

/**
 * 判断wifi是否连接状态
 * 需添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 */
public static boolean isWifiConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}

获取移动网络运营商名称

/**
 * 获取移动网络运营商名称
 * 如中国联通、中国移动、中国电信
 */
public static String getNetworkOperatorName(Context context) {
    TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    return tm != null ? tm.getNetworkOperatorName() : null;
}

获取移动终端类型

/**
 * 获取移动终端类型
 * PHONE_TYPE_NONE  : 0 手机制式未知
 * PHONE_TYPE_GSM   : 1 手机制式为GSM,移动和联通
 * PHONE_TYPE_CDMA  : 2 手机制式为CDMA,电信
 * PHONE_TYPE_SIP   : 3
 */
public static int getPhoneType(Context context) {
    TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    return tm != null ? tm.getPhoneType() : -1;
}

获取连接的网络类型(2G,3G,4G)

/**
 * 获取连接的网络类型(2G,3G,4G)
 * 联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO
 */
public static int getNetTpye(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    switch (telephonyManager.getNetworkType()) {
        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:
            return Constants.NETWORK_CLASS_2_G;
        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:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            return Constants.NETWORK_CLASS_3_G;
        case TelephonyManager.NETWORK_TYPE_LTE:
            return Constants.NETWORK_CLASS_4_G;
        default:
            return Constants.NETWORK_CLASS_UNKNOWN;
    }
}
public class Constants {
    // Unknown network class
    public static final int NETWORK_CLASS_UNKNOWN = 0;
    // wifi network
    public static final int NETWORK_WIFI = 1;
    // "2G" networks
    public static final int NETWORK_CLASS_2_G = 2;
    // "3G" networks
    public static final int NETWORK_CLASS_3_G = 3;
    // "4G" networks
    public static final int NETWORK_CLASS_4_G = 4;
}

获取当前手机的网络类型(WIFI,2G,3G,4G)

/**
 * 获取当前手机的网络类型(WIFI,2G,3G,4G)
 * 需添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 * 需要用到上面的方法
 */
public static int getCurNetworkType(Context context) {
    int netWorkType = Constants.NETWORK_CLASS_UNKNOWN;
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        int type = networkInfo.getType();
        if (type == ConnectivityManager.TYPE_WIFI) {
            netWorkType = Constants.NETWORK_WIFI;
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            netWorkType = getNetworkTpye(context);
        }
    }
    return netWorkType;
}