Android - How to place a call through specific sim in dual sim phone?

人走茶凉 提交于 2020-01-02 15:27:42

问题


I am not able to set the default sim for calling. I am trying to alter the system settings to change the default sim every before I sent the ACTION_CALL intent but ever time I am getting the sim selection dialog

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;
}

}

My intention is to place the call through a specific sim using Android 5.1 APIs. Please let me know if there is any alternate approach.


回答1:


From API level 22 and above we can set sim selection while making a call Intent by using SubscriptionManager and TelecomManager as follows.

    //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);

For more details refer https://developer.android.com/reference/android/telephony/SubscriptionManager.html Also above code will work only for API 22 and above and require READ_PHONE_STATE permissions




回答2:


dual-sim is not official supports of Android SDK. I suggest you to capture the logcat and make a specific sim call with system dialer, finding out the intent format about the sim cards.

UPDATE

from api level 22, the sdk supports dual sim link but i can not find the ACTION_CALL intent format for dual-sim in SDK documents.




回答3:


True-caller app has been able to add a button for selecting a specified sim to either send or receive voice and text messages. Have tried SubscriptionManager API introduced in 5.1 but still getting no change.



来源:https://stackoverflow.com/questions/33451660/android-how-to-place-a-call-through-specific-sim-in-dual-sim-phone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!