Android 在双 SIM 卡中发送短信 Phone

Android send sms in Dual SIM Phone

我正在使用包括直接使用 SIM 1 或 SIM 2 发送短信的应用程序。默认设置哪个 SIM 卡并不重要请帮助。谢谢

例如,如果 SIM 1 默认设置,我需要从 sim 2 发送短信怎么办。

我试过了

private void SendSMS(final String phoneNo, final String msg) {
        sentStatusReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                String s = "Unknown Error";
                boolean success = false;
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        success = true;
                        s = "Message Sent Successfully !!";
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        s = "Generic Failure Error";
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        s = "Error : No Service Available";
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        s = "Error : Null PDU";
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        s = "Error : Radio is off";
                        break;
                    default:
                        break;
                }
                Toast.makeText(context_, s, Toast.LENGTH_SHORT).show();
                getContext().unregisterReceiver(sentStatusReceiver);
                if (success) {
                    SmsActionDialog.this.dismiss();
                } else {
                    ShowAlertBox("I tried to send SMS but failed", "The SMS app will be opened to send the message.Continue?", msg);
                }
            }
        };
        getContext().registerReceiver(sentStatusReceiver, new IntentFilter("SMS_SENT"));
        try {
            final SmsManager smsManager = SmsManager.getDefault();
            final PendingIntent sentIntent = PendingIntent.getBroadcast(getContext(), 0, new Intent("SMS_SENT"), 0);
//            if (msg.length() > 160) {
//                final ArrayList<String> messagelist = smsManager.divideMessage(msg);
//                final ArrayList<PendingIntent> sentPendingIntents = new ArrayList<>();
//                for (int i = 0; i < messagelist.size(); i++) {
//                    sentPendingIntents.add(i, sentIntent);
//                }
//
//                smsManager.sendMultipartTextMessage(phoneNo, null, messagelist, sentPendingIntents, null);
//            } else {
            smsManager.sendTextMessage(phoneNo, null, msg, sentIntent, null);
//            }
        } catch (Exception ex) {
            Toast.makeText(context_, ex.getMessage(), Toast.LENGTH_LONG).show();
            ShowAlertBox("I tried to send SMS but failed", "The SMS app will be opened to send the message.Continue?", msg);
            ex.printStackTrace();
        }
    }

一些设备面临一般故障错误。如何解决?

尝试以下方法,其中 simIndex 对于 sim10,对于 sim21

 public void sendSMS(final String number,final String text)
    {
        final PendingIntent localPendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
        final PendingIntent localPendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);

        if (Build.VERSION.SDK_INT >= 22)
        {

            SubscriptionManager subscriptionManager=((Activity)mContext).getSystemService(SubscriptionManager.class);
            SubscriptionInfo subscriptionInfo=subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(simIndex);
            SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
        }
        SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
    }