Android 5.1以上双卡指定sim卡拨打电话

本文提供了一段用于指定SIM卡拨打电话的代码实现,并详细解释了如何根据SIM卡槽位ID选择正确的PhoneAccountHandle来发起呼叫。

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

直接上代码:


    /**
     * 指定sim卡拨打电话
     *
     * @param phoneNumber
     * @param slotId      0:卡1  1:卡2
     */
  public void callPhone(String phoneNumber, int slotId) {
        LogUtil.d(TAG, "call phone : phoneNumber=" + phoneNumber + ", slotId=" + slotId);
        PhoneAccountHandle phoneAccountHandle = getPhoneAccountHandle(slotId);
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
        intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }
/**
    这一块首先获取手机中所有sim卡 PhoneAccountHandle 每一个 PhoneAccountHandle 表示一个sim卡, 然后根据 slotId 判断所指定的sim卡并返回此 PhoneAccountHandle (这里5.1 和 6.0需要区分对待)
*/
@Target Api(Build.VERSION_CODES.M)
    private PhoneAccountHandle getPhoneAccountHandle(int slotId) {
        TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
        //PhoneAccountHandle api>5.1
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            List<PhoneAccountHandle> handles = (List<PhoneAccountHandle>) ReflectUtil.invokeMethod(tm, "getCallCapablePhoneAccounts");
            SubscriptionManager sm = SubscriptionManager.from(mContext);
            if (handles != null) {
                for (PhoneAccountHandle handle : handles) {
                    SubscriptionInfo info = sm.getActiveSubscriptionInfoForSimSlotIndex(slotId);
                    if (info != null) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            if (TextUtils.equals(info.getIccId(), handle.getId())) {
                                LogUtil.d(TAG, "getPhoneAccountHandle for slot" + slotId + " " + handle);
                                return handle;
                            }
                        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                            if (TextUtils.equals(info.getSubscriptionId() + "", handle.getId())) {
                                LogUtil.d(TAG, "getPhoneAccountHandle for slot" + slotId + " " + handle);
                                return handle;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

以上就是指定sim卡拨打的全部代码, 不擅长写博客, 希望对有需要的童鞋有所帮助.

获取双卡 Android 手机的电话号码可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String sim1PhoneNumber = telephonyManager.getLine1Number(0); String sim2PhoneNumber = telephonyManager.getLine1Number(1); ``` 需要注意的是,有些运营商可能不会将电话号码存储在 SIM 中,因此可能无法获取到电话号码。 获取双卡 Android 手机的运营商频段可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo(); for (CellInfo cellInfo : cellInfoList) { if (cellInfo instanceof CellInfoGsm) { CellIdentityGsm cellIdentityGsm = ((CellInfoGsm) cellInfo).getCellIdentity(); int mcc = cellIdentityGsm.getMcc(); int mnc = cellIdentityGsm.getMnc(); int arfcn = cellIdentityGsm.getArfcn(); // TODO: 处理 GSM 频段信息 } else if (cellInfo instanceof CellInfoCdma) { CellIdentityCdma cellIdentityCdma = ((CellInfoCdma) cellInfo).getCellIdentity(); int sid = cellIdentityCdma.getSystemId(); int nid = cellIdentityCdma.getNetworkId(); int bid = cellIdentityCdma.getBasestationId(); int freq = cellIdentityCdma.getFrequency(); // TODO: 处理 CDMA 频段信息 } else if (cellInfo instanceof CellInfoLte) { CellIdentityLte cellIdentityLte = ((CellInfoLte) cellInfo).getCellIdentity(); int mcc = cellIdentityLte.getMcc(); int mnc = cellIdentityLte.getMnc(); int earfcn = cellIdentityLte.getEarfcn(); // TODO: 处理 LTE 频段信息 } } ``` 需要注意的是,以上代码需要在 Android 5.1 及以上的版本上运行,并且需要在 AndroidManifest.xml 文件中添加相应的权限: ```xml <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值