Android之获取移动网络运营商名称

本文介绍了一种适用于安卓10.0设备的新型方法,用于获取移动网络运营商名称,如中国移动、中国联通、中国电信等。该方法通过读取SIM卡运营商代码并映射到运营商名称实现,需申请READ_PHONE_STATE权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原来的获取方法不好用了,适配安卓10.0设备,最新获取移动网络运营商名称方法。

获取权限:

android.permission.READ_PHONE_STATE

工具类代码:

/**
 * Created by zachary on 2020/04/02.
 * 获取设备信息
 */
public class DeviceUtil {

    /**
     * 获取网络运营商名称
     * <p>中国移动、如中国联通、中国电信</p>
     *
     * @return 运营商名称
     */
    public static String getNetworkOperatorName() {
        String  opeType = "unknown";
        // No sim
        if (!hasSim(BaseApplication.getApplication().getApplicationContext())) {
            return opeType;
        }

        TelephonyManager tm = (TelephonyManager) BaseApplication.getApplication().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String operator = tm.getSimOperator();
        if ("46001".equals(operator) || "46006".equals(operator) || "46009".equals(operator)) {
            opeType = "中国联通";
        } else if ("46000".equals(operator) || "46002".equals(operator) || "46004".equals(operator) || "46007".equals(operator)) {
            opeType = "中国移动";

        } else if ("46003".equals(operator) || "46005".equals(operator) || "46011".equals(operator)) {
            opeType = "中国电信";
        } else {
            opeType = "unknown";
        }
        return opeType;
    }

    /**
     * 检查手机是否有sim卡
     */
    private static boolean hasSim(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = tm.getSimOperator();
        if (TextUtils.isEmpty(operator)) {
            return false;
        }
        return true;
    }

    /**
     * 判断数据流量开关是否打开
     *
     * @param context
     * @return
     */
    public static boolean isMobileDataEnabled(Context context) {
        try {
            Method method = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true);
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            return (Boolean) method.invoke(connectivityManager);
        } catch (Throwable t) {
            return false;
        }
    }

}

注:中国铁通:46020

推荐文章:Android获取手机运营商、有无sim卡

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值