Android 9.0 双卡拨号

本文介绍如何在Android9.0环境下实现双SIM卡设备的指定卡拨号功能,包括检测SIM卡数量、指定SIM卡进行拨号、获取SIM卡颜色标识及确定最近一次通话所使用的SIM卡。

Android 9.0 双卡拨号 红米8A 双电信卡测试指定卡拨打通过

相关 api

getCallCapablePhoneAccounts
Added in API level 23

Android 5.0 之前的版本

Call from second sim

获取 sim 卡数量

public static boolean isMultiSim(Context context){
    boolean result = false;
    TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
    if(telecomManager != null){
        List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
        result = phoneAccountHandleList.size() >= 2;
    }
    return result;
}

用指定 sim 卡拨号

public static void call(Context context, int id, String telNum){
    TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);

    if(telecomManager != null){
        List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + telNum));
        intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandleList.get(id));
        context.startActivity(intent);
    }
}

获取卡的颜色

public static int getSimColor(Context context, int id){
    int highlightColor = 0;
    TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
    if(telecomManager != null) {
        List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();

        PhoneAccount phoneAccount = telecomManager.getPhoneAccount(phoneAccountHandleList.get(id));
        if (phoneAccount != null) {
            highlightColor = phoneAccount.getHighlightColor();
        }
    }
    return highlightColor;
}

获取最近一次通话使用的 sim 卡

public static String getLastestSim(Context context, String telNum){
    String result = "SIM1";
    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.NUMBER, CallLog.Calls.PHONE_ACCOUNT_ID},
                CallLog.Calls.NUMBER + " = ?", new String[]{telNum}, CallLog.Calls.DEFAULT_SORT_ORDER);
        if (cursor != null && cursor.moveToFirst()) {
            int subId = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID));
            Logger.d(TAG, "getLastestSim subId:" + subId);
            int slotId = getSlotIdUsingSubId(subId, context);
            Logger.d(TAG, "getLastestSim slotId:" + slotId);
            if(1 == slotId){
                result = "SIM2";
            }
        }
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        if(cursor != null){
            cursor.close();
        }
    }

    Logger.d(TAG, "getLastestSim result:" + result);

    return result;
}

用 SubscriptionId 获取 slot_id

public static int getSlotIdUsingSubId(int subId,Context context) throws InvocationTargetException {
    int  result = 0;
    try {
        Class<?> clz = Class.forName(SUBSCRIPTION_MANAGER);
        Object subSm;
        Constructor<?> constructor = clz.getDeclaredConstructor(Context.class);
        subSm  = constructor.newInstance(context);
        Method mth = clz.getMethod("getSlotId", int.class);
        result = (int)mth.invoke(subSm, subId);

    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
            | IllegalArgumentException | NoSuchMethodException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return result;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值