【Code】Android Telephony 获取SubscriptionManager和TelephonyManager

背景

早期的手机只有单卡 ,基本用默认卡(代码如下),那么双卡手机的业务逻辑就会存在问题。

//手动搜网的功能案例,根据卡槽/Phone对象直接获取信息

private Context mcontext = context;
private Phone mPhone = PhoneFactory.getPhone(0);    //默认Phone
//只搜索默认卡的网络模式
if (phone != null) {
    phone.getNetworkSelectionMode(msg);
}


private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
mSubId = mSubscriptionManager.getDefaultDataSubscriptionId();    //默认数据卡

为适配双卡,则需要正确获取和传递subId等关键信息。

源码API

很多APP中还是使用from()获取subManager,其实这等同于直接获取系统服务的接口。

按照性能考虑,不如直接getSystemService就好了。

 SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
SubscriptionManager 源码接口
//android-34\android\telephony\SubscriptionManager.java

    /**
     * @deprecated developers should always obtain references directly from
     *             {@link Context#getSystemService(Class)}.
     */
    @Deprecated
    public static SubscriptionManager from(Context context) {
        return (SubscriptionManager) context
                .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    }

TelephonyManager也是一样的逻辑

  private mTelephonyManager = TelephonyManager.from(context);
TelephonyManager.java 源码接口
//PATH:android-34\android\telephony\TelephonyManager.java
    //Android P之前使用
    /** {@hide} */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    public static TelephonyManager from(Context context) {
        return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }


    //现在常用,根据卡(注册信息)获取相应的TM,
    /**
     * Create a new TelephonyManager object pinned to the given subscription ID.
     *
     * @return a TelephonyManager that uses the given subId for all calls.
     */
    public TelephonyManager createForSubscriptionId(int subId) {
      // Don't reuse any TelephonyManager objects.
      return new TelephonyManager(mContext, subId);
    }

适配方案

根据注册信息去拿Phone对象,继而执行对应卡的业务。

private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
private int mPhoneId = -1;


//TODO: 可优化,遍历所有注册列表,而不是用默认数据卡
mSubId = mSubscriptionManager.getDefaultDataSubscriptionId();

//根据注册信息获取Phone对象
if (!SubscriptionManager.isValidPhoneId(mPhoneId)) {
    mPhoneId = SubscriptionManager.getPhoneId(mSubId);
    Log.d(TAG, "onReceive, mPhoneId = " + mPhoneId);
    if (SubscriptionManager.isValidPhoneId(mPhoneId)) {
        mPhone = PhoneFactory.getPhone(mPhoneId);
        Log.d(TAG,"onReceive, update phone");
    }
}

获取当前注册列表

mSubscriptionManager.getActiveSubscrip

循环卡槽获取subid

private void getSubInfo() {
    //返回设备中当前可用的移动网络数量,多卡设备上通常为2或日后更多(双卡)
    int phoneCount = TelephonyManager.getDefault().getPhoneCount();
    if (phoneCount > 1) {
        // 当前设备支持双卡或多卡
	// TODO: 处理双卡或多卡的情况
    } else {
        // 当前设备只支持单卡
        // TODO: 处理单卡的情况
    }

    for (int i = 0; i < phoneCount; i ++) {
        //获取指定卡槽的注册信息。
        final SubscriptionInfo subInfo  =  
            mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(i);
	if (subInfo == null) {
	    continue;
	}
	int subId = subInfo.getSubscriptionId();
    
	//获取对应SIM的CarrierConfig配置。
	PersistableBundle mCarrierConfig = mConfigManager.getConfigForSubId(subId);
	if (mCarrierConfig == null) {
	    return;
	}
    }
}

需要校验权限,如果应用已经有此权限,可以不用按提示添加try catch也能正常运行的。

【API】SubscriptionManager.getActiveSubscriptionInfoList
【API】SubscriptionManager.getActiveSubscriptionInfoList
【Code】校验权限
【Code】check permission READ_PHONE_STATE 

权限校验的代码:

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

        List<SubscriptionInfo> subscriptionInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();

相关介绍

Android 双卡适配 subId 相关方法-CSDN博客

【笔记】Android Telephony | SIM 卡管理和subId、slotId、phoneId 定义关系说明_subid phoneid-CSDN博客

接口介绍:TelephonyManager.java

https://developer.android.com/reference/android/telephony/TelephonyManager

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值