Android - 如何通过双卡手机中的特定 SIM 卡调用电话?

标签 android dual-sim

我无法为通话设置默认 SIM 卡。在发送 ACTION_CALL Intent 之前,我试图更改系统设置以更改默认 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;
}

我的 Intent 是通过使用 Android 5.1 API 的特定 sim 卡调用电话。如果有任何替代方法,请告诉我。

最佳答案

从 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 权限

关于Android - 如何通过双卡手机中的特定 SIM 卡调用电话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451660/

相关文章:

Android - 仅查询特定文件夹中的音频文件(不包括子文件夹)

android - 通过 httpurlconnection 更改 HttpPost

android - 如何从菜单(onOptionsItemSelected)中缩小/隐藏(不可见)ViewStub?

android - 查看手机SIM槽总数

android - 从第二个 sim 调用

Android - LinkedHashMap无法在Activity之间传递

java - 单击时更改图像按钮系统资源

android-contacts - 通过Android的通话记录获取通话中使用的SIM卡号码

android - SubscriptionInfo.getMnc() 为具有不同运营商的两个 SIM 返回相同的值