简体   繁体   中英

Send SMS from one SIM in dual sim moble phones

When I tried to send SMS from a dual SIM android device from an application using SMS Manager it is sending SMS from both the SIM. Can you help me with the solution.

private void sendSMS(String phoneNumber)
    {       
        String msg="app Test";
        try {
             SmsManager smsManager = SmsManager.getDefault();
             smsManager.sendTextMessage(phoneNumber, null, msg, null, null);
            // this.stopSelf();
             Toast.makeText(CallBlockingService.this, "SMS sent.",
             Toast.LENGTH_LONG).show();

          } catch (Exception e) 
        {
             Toast.makeText(CallBlockingService.this,
             "SMS faild, please try again.",
             Toast.LENGTH_LONG).show();
             e.printStackTrace();
          }       
    }

If you can use this code for API level 22+. here 22- I'm using default SMS channel.

private void sendDirectSMS() {

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
        SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);

    // SEND BroadcastReceiver
    BroadcastReceiver sendSMS = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    showSnackBar(getString(R.string.sms_sent));
                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_SUCCESS);
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    showSnackBar(getString(R.string.sms_send_failed_try_again));
                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    showSnackBar(getString(R.string.no_service_sms_failed));
                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    showSnackBar(getString(R.string.no_service_sms_failed));
                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    showSnackBar(getString(R.string.no_service_sms_failed));
                    Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
                    break;
            }
        }
    };

    // DELIVERY BroadcastReceiver
    BroadcastReceiver deliverSMS = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), R.string.sms_delivered,
                        Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), R.string.sms_not_delivered,
                        Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    registerReceiver(sendSMS, new IntentFilter(SENT));
    registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
    String smsText = getSmsText();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionManager localSubscriptionManager = SubscriptionManager.from(context);
        if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
            List localList = localSubscriptionManager.getActiveSubscriptionInfoList();

            SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(0);
            SubscriptionInfo simInfo2 = (SubscriptionInfo) localList.get(1);

            //SendSMS From SIM One
            SmsManager.getSmsManagerForSubscriptionId(simInfo1.getSubscriptionId()).sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);

            //SendSMS From SIM Two
            SmsManager.getSmsManagerForSubscriptionId(simInfo2.getSubscriptionId()).sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);
        }
    } else {
        SmsManager.getDefault().sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);
        Toast.makeText(getBaseContext(), R.string.sms_sending, Toast.LENGTH_SHORT).show();
    }
}

Provide service center number of SIM you want to send SMS from. For one of my SIM service center number is +919892051914 and for another is +919022000500

Hence if I wanna send SMS from my one SIM then code will be

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, "+919892051914", msg, null, null);

That's it... Enjoy....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM