android send sms programatically from post paid service

228 Views Asked by At

I am sending sms programatically using the code

String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

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

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

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        

But If the mobile has postpaid network connection, message is not sending. How to handle this?

1

There are 1 best solutions below

0
On

I have had this same problem with my app. However, I figured out that if the message count is more than 160, sms.sendTextMessage() cannot work. ie. You have to divide the message into parts and then sms.sendMultiPartTextMessage(). Only English Characters will be <=160 to send text message. If you are using any special characters, the count(length) should be <=70. The following is the my code. I am using Arabic Characters.

private void sendSMS(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

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

    if (message.length() <= 70) {
        SmsManager sms = SmsManager.getDefault();
        Log.d(LOG_TAG, "Send Sms : " + message + " serverNumber "
                + phoneNumber);
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    } else {
        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts = sms.divideMessage(message);
        int numParts = parts.size();

        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

        for (int i = 0; i < numParts; i++) {
            sentIntents.add(PendingIntent.getBroadcast(act, 0, new Intent(
                    SENT), 0));
            deliveryIntents.add(PendingIntent.getBroadcast(act, 0,
                    new Intent(DELIVERED), 0));
        }
        Log.d(LOG_TAG, "Send Sms multipart : " + message + " serverNumber "
                + phoneNumber);
        sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
                deliveryIntents);
    }
}

I hope this helps.