Android指定sim卡拨打电话

1:申请权限

Manifest.permission.CALL_PHONE

2:创建intent

 Intent intent = new Intent(Intent.ACTION_CALL);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

3:获取 PhoneAccountHandle

above android.os.Build.VERSION_CODES.M 以上添加 PhoneAccountHandle。

  /**
     * 基于iccid 获取PhoneAccountHandle
     *
     * @param iccid sim卡iccid
     */
    public static PhoneAccountHandle getPhoneAccountHandle(String iccid) {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
            return null;
        }
        try {
            TelecomManager telecomManager = (TelecomManager) AppEnv.getContext().getSystemService(TELECOM_SERVICE);
            if (telecomManager == null) {
                return null;
            }
            List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
            if (phoneAccountHandleList == null || phoneAccountHandleList.isEmpty()) {
                return null;
            }
            for (PhoneAccountHandle handle : phoneAccountHandleList) {
                if (iccid.equalsIgnoreCase(handle.getId())) {
                    return handle;
                }
            }
        } catch (Throwable e) {
            Log.e(TAG, "getPhoneAccountHandle error:", e);
        }
        return null;
    }
		PhoneAccountHandle phoneAccountHandle = null;
        if (!TextUtils.isEmpty(iccId) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            phoneAccountHandle =getPhoneAccountHandle(iccId);
            if (phoneAccountHandle != null) {
                intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
            }
        }

4:添加soltId

PhoneAccountHandle==null 或者低于m .

 public static final String[] dualSimTypes = {
            "extra_asus_dial_use_dualsim",
            "com.android.phone.extra.slot",//基本上是这个
            "slot",
            "simslot",
            "sim_slot",
            "subscription",//华为是这个
            "Subscription",
            "phone",
            "com.android.phone.DialingMode",
            "simSlot",
            "slot_id",//小米是这个
            "simId",
            "simnum",
            "phone_type",
            "slotId",
            "slotIdx"
    };
 for (String dualSimType : dualSimTypes) {
                    //所有的可能都加上
                    intent.putExtra(dualSimType, slotId);
                }

5:拨打电话

      Uri data = Uri.parse("tel:" + phone);
            intent.setData(data);
            try {
                context.startActivity(intent);
                return true;
            } catch (Throwable e) {
               Log.e(TAG, "startCall error: ", e);
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值