Android: Send MMS programmatically from a specific SIM, in a dual SIM device

238 Views Asked by At

My code successfully sends MMS. The approach uses ConnectivityManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
    NetworkRequest.Builder builder = new NetworkRequest.Builder();

    builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
    builder.addCapability(NetworkCapabilities.NET_CAPABILITY_MMS);
    
    NetworkRequest networkRequest = builder.build();

    cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback()
    {
        @Override
        public void onAvailable(Network network)
        {
            super.onAvailable(network);
            // do MMS sending here using HTTP/network APIs
            cm.unregisterNetworkCallback(this);
        }
    });
}

In a dual SIM phone, this approach seems to pick a default SIM and send MMS out of that number (I am not sure how the default is picked).

How does one specify a SIM slot or a Subscription ID as part of the NetworkRequest? Or is there another way?

Just a reiterate this question is only about MMS, not SMS. Also, the question is not about sending MMS programmatically (that is discussed elsewhere in SO). The question is specific to dual SIM devices.

Thank you

1

There are 1 best solutions below

3
Lakpriya Senevirathna On

You can get sim info by below method

//above Android API 22
if (Build.VERSION.SDK_INT > 22) {
//for dual sim mobile
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);

if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
 //if there are two sims in dual sim mobile
    List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
    SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
    SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);

    final String sim1 = simInfo.getDisplayName().toString();
    final String sim2 = simInfo1.getDisplayName().toString();

}else{
 //if there is 1 sim in dual sim mobile
    TelephonyManager tManager = (TelephonyManager) getBaseContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    String sim1 = tManager.getNetworkOperatorName();

}

}else{
//below android API 22
        TelephonyManager tManager = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);

        String sim1 = tManager.getNetworkOperatorName();
}