Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Option to send sms using Sim1 or Sim2 programmatically

Tags:

I have an Android phone with 2 SIM card and I want to send sms using Sim1 or Sim2.By default the message is sent from sim1. But i want to send sms from sim2. Is it possible to setting to send sms using Sim1 or Sim2?

It would be great if there is an setting options to send sms using Sim1 or Sim2.. this is useful for dual SIM android phones. I created sms application android I've been able to sms application smoothly but default sms sent by SIM 1.But I want to send the sms programmatically by setting to send the sms by sim1 or sim2?

like image 670
Jebasuthan Avatar asked Jan 28 '14 15:01

Jebasuthan


People also ask

How can I send SMS from second SIM?

Tap on Text messages to select the default SIM when sending an SMS. Next, select one of the two SIM cards. Again, we recommend choosing the SIM card with the best plan for text messages.

How can I select SIM to send SMS?

From the Home screen, swipe up and then tap Settings > SIM cards. Tap SMS messages. Choose the card you want to use for sending SMS and MMS.

How can I receive messages on dual SIM?

Dual SIM Dual Standby (DSDS) You can receive calls and messages to both SIM cards. Before you can use the SIM cards, you have to enable them in the Dual SIM settings menu. Data traffic can only be handled on one SIM card at a time and you can select which SIM card you want to use.


2 Answers

If you can use this code for API level 22+.

private void sendDirectSMS() {

    private static String SENT = "SMS_SENT", DELIVERED = "SMS_DELIVERED";

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

Also add permission.

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
like image 81
Anjal Saneen Avatar answered Sep 19 '22 21:09

Anjal Saneen


SmsManager.getSmsManagerForSubscriptionId(int subscriptionId).sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent);

this can be working after 5.1 Lollipop version.before that we need to see more. once i got the answer i will update.

like image 37
Thiyagesh waran Avatar answered Sep 22 '22 21:09

Thiyagesh waran