4

我无法为通话设置默认 SIM 卡。我试图在每次发送 ACTION_CALL 意图之前更改系统设置以更改默认 sim,但每次我得到 sim 选择对话框

public class CallUtil {

public static void sendCallIntent(Context context, String number) {
    Intent intent = new Intent(Intent.ACTION_CALL)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setData(Uri.parse("tel:" + number));

    setDefaultSim(context);
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    context.startActivity(intent);
}

public static void setDefaultSim(Context context) {
    try {
        ContentValues val = new ContentValues();
        List<Integer> sims = getInsertedSIMIds(context);
        val.put("value", sims.get(0));
        context.getContentResolver().update(Uri.parse("content://settings/system"), val, "name='voice_call_sim_setting'", null);

    } catch (Exception e) {

    }

}

public static List<Integer> getInsertedSIMIds(Context context){
    List<Integer> list = new ArrayList<Integer>();
    SubscriptionManager  sm=(SubscriptionManager)context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    for ( SubscriptionInfo sub: sm.getActiveSubscriptionInfoList()) {
        list.add(sub.getSimSlotIndex());
    }
    return list;
}

}

我的意图是通过使用 Android 5.1 API 的特定 sim 进行呼叫。请让我知道是否有任何替代方法。

4

3 回答 3

5

从 API 级别 22 及更高级别开始,我们可以在使用 SubscriptionManager 和 TelecomManager 进行呼叫 Intent 时设置 sim 选择,如下所示。

    //To find SIM ID
    String primarySimId,secondarySimId;
    SubscriptionManager subscriptionManager = (SubscriptionManager) appContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List<SubscriptionInfo> subList = subscriptionManager.getActiveSubscriptionInfoList(); 
    int index=-1;
    for (SubscriptionInfo subscriptionInfo : subList) {
        index++;
        if(index == 0){
           primarySimId=subscriptionInfo.getIccId();
        }else {
           secondarySimId=subscriptionInfo.getIccId();
        }
    } 

    // TO CREATE PhoneAccountHandle FROM SIM ID  
    TelecomManager telecomManager =(TelecomManager) getSystemService(Context.TELECOM_SERVICE);
    List<PhoneAccountHandle> list = telecomManager.getCallCapablePhoneAccounts();
    PhoneAccountHandle primaryPhoneAccountHandle,secondaryPhoneAccountHandle;
    for(PhoneAccountHandle phoneAccountHandle:list){
       if(phoneAccountHandle.getId().contains(primarySimId)){
         primaryPhoneAccountHandle=phoneAccountHandle;
        }
       if(phoneAccountHandle.getId().contains(secondarySimId)){
         secondaryPhoneAccountHandle=phoneAccountHandle;                     
        }
    }

    //To call from SIM 1
    Uri uri = Uri.fromParts("tel",number, "");
    Bundle extras = new Bundle();  extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,primaryPhoneAccountHandle);
    telecomManager.placeCall(uri, extras);

    //To call from SIM 2
    Uri uri = Uri.fromParts("tel",number, "");
    Bundle extras = new Bundle();  extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,secondaryPhoneAccountHandle);
    telecomManager.placeCall(uri, extras);

有关更多详细信息,请参阅https://developer.android.com/reference/android/telephony/SubscriptionManager.html 以上代码仅适用于 API 22 及更高版本,并且需要 READ_PHONE_STATE 权限

于 2019-10-02T20:03:24.967 回答
0

True-caller 应用程序已经能够添加一个按钮,用于选择指定的 SIM 卡以发送或接收语音和文本消息。尝试了 5.1 中引入的 SubscriptionManager API,但仍然没有任何变化。

于 2017-07-22T14:05:28.853 回答
0

dual-sim 不是 Android SDK 的官方支持。我建议您捕获 logcat 并使用系统拨号器进行特定的 sim 调用,找出有关 sim 卡的意图格式。

更新

从 api 级别 22 开始,sdk 支持双卡链接,但我在 SDK 文档中找不到双卡的 ACTION_CALL 意图格式。

于 2016-01-19T09:31:50.680 回答