android判断网络连接状态、联网类型、运营商

/**
     * 获取上网方式
     * 
     * @param mContext
     * @return
     */
    public static String getNetType(Context mContext) {

        String netType = "";
        ConnectivityManager connectionManager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectionManager.getActiveNetworkInfo();
        if (null != info && info.isAvailable()) {
            netType = info.getTypeName();
        }
        return netType;
    }

    /**
     * 判断网络连接是否可用
     * 
     * @param mContext
     * @return 
     */

    public static boolean getNetIsVali(Context mContext) {

        if (mContext != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 获取运营商信息
     * 
     * @param mContext
     * @return
     */
    public static String getNetExtraInfo(Context mContext) {

        String netExtraInfo = "";
        TelephonyManager mTm = (TelephonyManager) mContext
                .getSystemService(Context.TELEPHONY_SERVICE);
        if (mTm.getSimState() == TelephonyManager.SIM_STATE_READY) {
            netExtraInfo = mTm.getSimOperator();
            if (null != netExtraInfo) {
                if (netExtraInfo.equals("46000")
                        || netExtraInfo.equals("46002")
                        || netExtraInfo.equals("46007")) {
                    // 中国移动
                    netExtraInfo = "中国移动";
                } else if (netExtraInfo.equals("46001")) {

                    // 中国联通
                    netExtraInfo = "中国联通";
                } else if (netExtraInfo.equals("46003")) {

                    // 中国电信
                    netExtraInfo = "中国电信";
                } else {
                    netExtraInfo = "其他";
                }
            }
        }
        return netExtraInfo;
    }

1.只考虑1卡

2.别忘了加网络权限

posted @ 2014-09-11 20:06  带狗狗带  Views(715)  Comments(0Edit  收藏  举报