Send MultipartTextMessge in Android with example


This is going to be a very simple tutorial that explains how to send multipartTextMessage in Android. For detailed explanation to send SMS see this tutorial.

            The maximum length of each SMS text message can vary by carrier, but are typically limited to 160 characters. As a result longer messages need to be broken into series of smaller parts. The SMS Manager includes the divideMessage method, which accepts a string as an input and breaks it into an ArrayList of messages, wherein each part is less than the maximum allowable size.

          We can then use sendMultipartTextMessage method on the SmsManager to transmit the array of messages.

ArrayList<String> msgArray=smsManager.divideMessage(msg);
smsManager.sendMultipartTextMessage(phoneNo, null, msgArray, null, null);

In the same way sentIntent and deliveryIntent parameters in the sendMultipartTextMessage method needs to be Array List. Thats is, very simple.


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:text="@string/pn"
        android:textColor="@android:color/white"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/phno"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:ems="10" >
    </EditText>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:text="@string/msg"
        android:textColor="@android:color/white"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/smstxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/b1" />

</LinearLayout>

MainActivity.java

package com.example.multipartsms;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
 Button btnSend;
 EditText etPhoneNo;
 EditText etMsg;

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  etPhoneNo = (EditText) findViewById(R.id.phno);
  etMsg = (EditText) findViewById(R.id.smstxt);
  btnSend = (Button) findViewById(R.id.send);

   btnSend.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    String phoneNo = etPhoneNo.getText().toString();
    String msg = etMsg.getText().toString();
    try {

      SmsManager smsManager = SmsManager.getDefault();
     ArrayList<String> msgArray = smsManager.divideMessage(msg);

      smsManager.sendMultipartTextMessage(phoneNo, null,
       msgArray, null, null);
     Toast.makeText(getApplicationContext(), "Message Sent",
       Toast.LENGTH_LONG).show();
    } catch (Exception ex) {
     Toast.makeText(getApplicationContext(),
       ex.getMessage().toString(), Toast.LENGTH_LONG)
       .show();
     ex.printStackTrace();
    }

    }
  });

  }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

Source Code of this application
MultipartSMS.rar

See how to receive SMS sent, delivery confirmation in Android here

 



Reactions:

1 comment :

  1. nice post,
    i try this and run well, but at receiver, Message not receive at one time? How to solve it?

    ReplyDelete