4

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.

Kamal
  • 41
  • 1
  • 3

3 Answers3

6

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

Ashi Agarwal
  • 91
  • 1
  • 4
  • Could you please give more detail concerning the nature of primarySimId and secondarySimId, e.g, string, int etc? I receive an error message saying cannot resolve primarySimId. What is primarySimId? – Yayayaya Dec 25 '19 at 20:06
  • primarySimId and secondarySimId are of type String, To resolve the error please define them at the start – Ashi Agarwal Dec 27 '19 at 05:27
  • I don't think `TelecomManager::getCallCapablePhoneAccounts()` and, `TelecomManager::placeCall(Uri, Bundle)` are available on API 22. Lint gives an error saying they are found in API 23 and above. – Brook MG Jul 28 '20 at 20:47
0

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.

Jiang YD
  • 3,205
  • 1
  • 14
  • 20
0

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.

Job M
  • 3,331
  • 2
  • 19
  • 26