Ranu
Ranu

Reputation: 1

Send sms in dual sim android phone by SmsManager

I would like to send sms by SmsManager its working but in first sim i have no balance in this case sms failed. so how to send sms by second sim only from my application. other app like whatsApp send msg at reg time from my second sim if not availible balance in first sim.so i think its possible. pls help me

String mobileNo = phoneNumber1.getText().toString();

  try {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(mobileNo, null, "BAL", null, null);
    Toast.makeText(getApplicationContext(), "SMS Sent!",
                Toast.LENGTH_LONG).show();
  } catch (Exception e) {
    Toast.makeText(getApplicationContext(),
        "SMS faild, please try again later!",
        Toast.LENGTH_LONG).show();
    e.printStackTrace();
  }

Upvotes: 0

Views: 5612

Answers (2)

Amit
Amit

Reputation: 31

I looked into SubscriptionManager class and found a method getActiveSubscriptionInfoForSimSlotIndex(simIndex) to get subscriptionInfo for perticular sim slot using this subscriptionInfo we can get subscriptionId of perticular simSlot. Using subscriptionID we can get SMSManager for desired simSlot

This worked for me:

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

Where simIndex is 0 for sim1 and 1 for sim2

Upvotes: 1

Janaka Bandara
Janaka Bandara

Reputation: 1072

After analyzing the logs of Mlais MX28 phone (with a customized ROM), I found the following trick to switch the SIM for SMS sending. Unfortunately, it seems to switch the SIM globally for all apps (I couldn't figure out a way to 'save and restore' the prevailing setting yet). Plus, you will probably have to go through the trouble of finding out the IDs (sim_id) assigned to each of your SIM cards (probably via checking the phone's log output; I couldn't find a programmatic way to do this, either).

int simId = <place desired SIM ID here>;
Intent simIntent = new Intent("android.intent.action.SMS_DEFAULT_SIM");
simIntent.putExtra("simid", simId);
sendBroadcast(simIntent);

I'm not sure if this would work for you (although the code seems manufacturer-independent); but it's still worth a shot, I suppose.

Update:

Strangely, the earlier approach ceased to work after some attempts. However, after some more log analysis I came across another approach which uses direct settings manipulation (which, unfortunately, requires the android.permission.WRITE_SETTINGS permission).

ContentValues val = new ContentValues();
val.put("value", "here goes the preferred SIM ID");
getContentResolver().update(Uri.parse("content://settings/system"), val, "name='sms_sim_setting'", null);

I believe it is adaptable for other multi-SIM-concerned operations as well, as the settings database on my device contains entries for settings like gprs_connection_sim_setting, voice_call_sim_setting, video_call_sim_setting, etc.

Upvotes: 0

Related Questions