获得sim卡是几G卡


import android.content.Context;
import android.telephony.TelephonyManager;
import android.widget.Toast;
/*
 * 采用几G通道通信,通信时采用的网络类型.
 * 几G的卡是移动运营商提供的移动通信通道类型,只有2G,3G。

 * 获取sim卡是几G卡,步骤:

 * 获得sim卡注册的移动运营商志识,获得移动运营商给sim卡提供的通信网络频段,然后判断是几G卡。

 *

 * 说明:

 * 移动数据图标上面的E,G,H,3G网络图标不是这样判断的。通话网络和移动数据上网网络的网络类型不同

*/

/*
 *  获得移动数据通道类型
 */
public class NetworkTypeUtils {

    public static final int NETWORK_TYPE_G = 0;
    public static final int NETWORK_TYPE_E = 1;
    public static final int NETWORK_TYPE_H = 2;
    public static final int NETWORK_TYPE_3G = 3;
    public static final int UNKNOWN_NETWORK_TYPE = -1;
    static boolean mHspaDataDistinguishable = true;

    public static int getNetworkType(Context context) {
        try {
            mHspaDataDistinguishable = context.getResources().getBoolean(
                    R.bool.config_hspa_data_distinguishable);
        } catch (Exception e) {
            mHspaDataDistinguishable = false;
        }
        
        TelephonyManager manager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        int networkType = manager.getNetworkType();
        if (networkType == TelephonyManager.NETWORK_TYPE_GPRS
                || networkType == TelephonyManager.NETWORK_TYPE_CDMA
                || networkType == TelephonyManager.NETWORK_TYPE_IDEN) {
            //target = "2G";
            return NETWORK_TYPE_G;
        } else if (networkType == TelephonyManager.NETWORK_TYPE_EDGE
                || networkType == TelephonyManager.NETWORK_TYPE_1xRTT) {
            //target = "2.75G";
            return NETWORK_TYPE_E;
        } else if (networkType == TelephonyManager.NETWORK_TYPE_UMTS
                || networkType == TelephonyManager.NETWORK_TYPE_EVDO_0
                || networkType == TelephonyManager.NETWORK_TYPE_EVDO_A
                || networkType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
            //target = "3G";
            return NETWORK_TYPE_3G;
        } else if (networkType == TelephonyManager.NETWORK_TYPE_HSDPA
                || networkType == TelephonyManager.NETWORK_TYPE_HSUPA
                || networkType == TelephonyManager.NETWORK_TYPE_HSPA) {
            if (mHspaDataDistinguishable) {
                //target = "3.5G";
                return NETWORK_TYPE_H;
            } else {
                //target = "3G";
                return NETWORK_TYPE_3G;
            }
        } else {
            //target = "未知网络";
            return UNKNOWN_NETWORK_TYPE;
        }
    }
}

 判断sim卡是几G卡的方法:

private boolean is3GSignal() {
            if (serviceState != null) {
                try {
                    String method = "getRadioTechnology";
                    Method mMethod = ServiceState.class.getMethod(method, null);
                    int networkType = (Integer) mMethod.invoke(serviceState, null);

                    //   由于这些静态属性都是hide注释的,所以可以通过反射来得到
                    Field f1 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_UMTS");
                    Field f2 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_HSDPA");
                    Field f3 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_HSUPA");
                    Field f4 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_HSPA");
                    Field f5 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_EVDO_0");
                    Field f6 = ServiceState.class.getDeclaredField("RADIO_TECHNOLOGY_EVDO_A");
                    f1.setAccessible(true);
                    f2.setAccessible(true);
                    f3.setAccessible(true);
                    f4.setAccessible(true);
                    f5.setAccessible(true);
                    f6.setAccessible(true);
                    
                    if (networkType == f1.getInt(serviceState)
                            || networkType == f2.getInt(serviceState)
                            || networkType == f3.getInt(serviceState)
                            || networkType == f4.getInt(serviceState)
                            || networkType == f5.getInt(serviceState)
                            || networkType == f6.getInt(serviceState)) {
                        return true;
                    }
                    Log.i("song", "3GSignal:" + networkType);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
            return false;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值