Android 6.0 双卡拨号

相关 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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,172评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,663评论 2 45
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,324评论 19 139
  • 本来看到边框好看,画完之后却突然发现,不知道在边框里应该画什么,画到最后结果成了一副黑板报。
    人生只若初见kl阅读 2,166评论 0 1
  • 你说你会爱我,只是时间问题罢了。 “陌陌,你还在爱他吗?”这是多年未见朋友半夜发给我的一条信息。 ...
    樊竹玖阅读 2,905评论 0 1