SMS not sending from some dual sim device android programmatically

1.1k views Asked by At

I am stuck for a long time and can't solve it. Please help... I am trying to send sms programmatically from android device. I can send successfully from many devices but can't from some Samsung dual sim devices with android api level >=22. For api level >=22 I have coded like,

public static final String SMS_SENT_ACTION = "package.name.app.SMS_SENT_ACTION";

registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        unregisterReceiver(this);
        String message = null;
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                message = "Message Sent Successfully !";
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                message = "Error.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                message = "Error: No service.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                message = "Error: Null PDU.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                message = "Error: Radio off.";
                sendDefaultMessage(default_number, default_send_number, text);
                break;
        }

        Toast.makeText(GetAppointment.this, message, Toast.LENGTH_LONG).show();

    }
}, new IntentFilter(SMS_SENT_ACTION));


SmsManager.getSmsManagerForSubscriptionId(subscription_id).sendTextMessage(send_number, null, messageToSend, PendingIntent.getBroadcast(
                    this, 0, new Intent(SMS_SENT_ACTION), 0), null);

There is no permission related issue because sms can be sent from asus dual sim device and sony single sim device with api level >=22.

How can I make it work for all device? I need help badly.

2

There are 2 answers

3
Jaymin Panchal On

You need to provide service center number of SIM you want to send SMS from in your sendTextMessage() as second parameter like,

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, "+9198********", msg, null, null);
0
Rahul Karande On

Then check sim settings using following function:

public int  getDefaultSimmm(Context context) {

Object tm = context.getSystemService(Context.TELEPHONY_SERVICE);
Method method_getDefaultSim;
int defaultSimm = -1;
try {
    method_getDefaultSim = tm.getClass().getDeclaredMethod("getDefaultSim");
    method_getDefaultSim.setAccessible(true);
    defaultSimm = (Integer) method_getDefaultSim.invoke(tm);
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Method method_getSmsDefaultSim;
int smsDefaultSim = -1;
try {
    method_getSmsDefaultSim = tm.getClass().getDeclaredMethod("getSmsDefaultSim");
    smsDefaultSim = (Integer) method_getSmsDefaultSim.invoke(tm);
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return smsDefaultSim;
}

Then following function send sms:

public boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) {
    String name;

    try {
        if (simID == 0) {
            name = "isms";
            // for model : "Philips T939" name = "isms0"
        } else if (simID == 1) {
            name = "isms2";
        } else {
            throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values");
        }
        Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
        method.setAccessible(true);
        Object param = method.invoke(null, name);

        method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", IBinder.class);
        method.setAccessible(true);
        Object stubObj = method.invoke(null, param);
        if (Build.VERSION.SDK_INT < 18) {
            method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
            method.invoke(stubObj, toNum, centerNum, smsText, sentIntent, deliveryIntent);
        } else {
            method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
            method.invoke(stubObj, ctx.getPackageName(), toNum, centerNum, smsText, sentIntent, deliveryIntent);
        }

        return true;
    } catch (ClassNotFoundException e) {
        Log.e("apipas", "ClassNotFoundException:" + e.getMessage());
    } catch (NoSuchMethodException e) {
        Log.e("apipas", "NoSuchMethodException:" + e.getMessage());
    } catch (InvocationTargetException e) {
        Log.e("apipas", "InvocationTargetException:" + e.getMessage());
    } catch (IllegalAccessException e) {
        Log.e("apipas", "IllegalAccessException:" + e.getMessage());
    } catch (Exception e) {
        Log.e("apipas", "Exception:" + e.getMessage());
    }
    return false;
}

I hope it will help you