Android:在双SIM卡设备中以编程方式从特定的SIM发送MMS

问题描述

我的代码成功发送了彩信。该方法使用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);
        }
    });
}

在双SIM卡电话中,这种方法似乎是选择默认的SIM卡,然后从该号码中发送彩信(我不确定如何选择默认值)。

如何将SIM插槽或订阅ID指定为NetworkRequest的一部分?还是有其他方法?

只需重申一下,此问题仅与MMS有关,与SMS无关。同样,问题不在于以编程方式发送MMS(在SO的其他地方讨论过)。该问题仅适用于双SIM卡设备。

谢谢

解决方法

您可以通过以下方法获取SIM卡信息

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...