Example usage for android.telephony SmsManager divideMessage

List of usage examples for android.telephony SmsManager divideMessage

Introduction

In this page you can find the example usage for android.telephony SmsManager divideMessage.

Prototype

public ArrayList<String> divideMessage(String text) 

Source Link

Document

Divide a message text into several fragments, none bigger than the maximum SMS message size.

Usage

From source file:com.hhunj.hhudata.SearchBookContentsActivity.java

private void sendSMS(String phoneNumber, String message) {
    // ---sends an SMS message to another device---
    SmsManager sms = SmsManager.getDefault();
    String SENT_SMS_ACTION = "SENT_SMS_ACTION";
    String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

    // create the sentIntent parameter
    Intent sentIntent = new Intent(SENT_SMS_ACTION);
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, 0);

    // create the deilverIntent parameter
    Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
    PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0, deliverIntent, 0);

    // register the Broadcast Receivers
    registerReceiver(new BroadcastReceiver() {
        @Override/*  w w  w. j  a v  a 2s.  c o m*/
        public void onReceive(Context _context, Intent _intent) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent success actions", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "SMS generic failure actions", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "SMS radio off failure actions", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "SMS null PDU failure actions", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(SENT_SMS_ACTION));

    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context _context, Intent _intent) {
            Toast.makeText(getBaseContext(), "SMS delivered actions", Toast.LENGTH_SHORT).show();
        }
    }, new IntentFilter(DELIVERED_SMS_ACTION));

    // if message's length more than 70 ,
    // then call divideMessage to dive message into several part ,and call
    // sendTextMessage()
    // else direct call sendTextMessage()
    if (message.length() > 70) {
        ArrayList<String> msgs = sms.divideMessage(message);
        for (String msg : msgs) {
            sms.sendTextMessage(phoneNumber, null, msg, sentPI, deliverPI);
        }
    } else {
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliverPI);
    }
}

From source file:com.hhunj.hhudata.ForegroundService.java

private void sendSMS(String message) {

    if (message == "")
        return;//from   ww w  .ja v a 2s .c  o m

    //String stext  =  "  " +m_contactmap.size();

    //notification ,title
    showNotification("hhudata", message);

    if (m_alarmInWorktime) {//
        Date dt = new Date();
        if (IsDuringWorkHours(dt) == false) {
            return;
        } else {
            //alarm.

        }
    }

    if (message.length() > 140) {
        //....
        return;
    }

    // ---sends an SMS message to another device---
    SmsManager sms = SmsManager.getDefault();
    String SENT_SMS_ACTION = "SENT_SMS_ACTION";
    String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

    // create the sentIntent parameter
    Intent sentIntent = new Intent(SENT_SMS_ACTION);
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, 0);

    // create the deilverIntent parameter
    Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
    PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0, deliverIntent, 0);

    // register the Broadcast Receivers
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context _context, Intent _intent) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent success actions", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "SMS generic failure actions", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "SMS radio off failure actions", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "SMS null PDU failure actions", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(SENT_SMS_ACTION));
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context _context, Intent _intent) {
            Toast.makeText(getBaseContext(), "SMS delivered actions", Toast.LENGTH_SHORT).show();
        }
    }, new IntentFilter(DELIVERED_SMS_ACTION));

    // if message's length more than 70 ,
    // then call divideMessage to dive message into several part ,and call
    // sendTextMessage()
    // else direct call sendTextMessage()

    Iterator it = m_contactmap.keySet().iterator();
    while (it.hasNext()) {
        ContactColumn.ContactInfo info = m_contactmap.get(it.next());
        if (info.mobile == null)
            continue;
        String mobile = info.mobile.trim();
        if (mobile.length() >= 4) {
            if (message.length() > 70) {

                ArrayList<String> msgs = sms.divideMessage(message);
                for (String msg : msgs) {
                    sms.sendTextMessage(mobile, null, msg, sentPI, deliverPI);
                }
            } else {
                sms.sendTextMessage(mobile, null, message, sentPI, deliverPI);
            }
        }
    }

}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc/*from  w  w  w  .  ja v a2s . c o  m*/
 */
public void sendSMS(final String phoneNumber, final String message, boolean i) throws IOException {
    if (!checkForPermission(Manifest.permission.SEND_SMS, "This is required to send a SMS")) {
        return;
    }
    if (!checkForPermission(Manifest.permission.READ_PHONE_STATE, "This is required to send a SMS")) {
        return;
    }
    if (i) {
        Intent smsIntent = null;
        if (android.os.Build.VERSION.SDK_INT < 19) {
            smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("address", phoneNumber);
            smsIntent.putExtra("sms_body", message);
        } else {
            smsIntent = new Intent(Intent.ACTION_SENDTO);
            smsIntent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
            smsIntent.putExtra("sms_body", message);
        }
        getContext().startActivity(smsIntent);

    } else {
        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts = sms.divideMessage(message);
        sms.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
    }
}