Android在双卡手机中发送短信

时间:2018-06-14 05:58:04

标签: android smsmanager dual-sim

我正在使用应用程序,其中包括使用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();
        }
    }

某些设备面临一般性故障错误。怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

请尝试以下方法simIndex 0sim11sim2

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