小工具知识网

当前位置: 首页 > 行业知识 > 【笔记】Android Telephony 获取SubscriptionManager和TelephonyManager

行业知识

【笔记】Android Telephony 获取SubscriptionManager和TelephonyManager

2026-10-31 22:32:14 来源:互联网转载

背景

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

()
//手动搜网的功能案例,根据卡槽/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   

相关介绍

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

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

上一篇:轻松掌握胡萝卜绘画技巧,画出诱人胡萝卜图像!

下一篇:没服务器怎么存账号?