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

来源:互联网 发布:阿里云 云上数据安全 编辑:程序博客网 时间:2024/06/11 13:42

直接上代码:

    /**     * 指定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卡拨打的全部代码, 不擅长写博客, 希望对有需要的童鞋有所帮助.

0 0
原创粉丝点击