Android Tutorials

Google+

Tuesday 14 January 2014

Sending SMS Message In Android



In This Tutorial we show the basic example of  sending SMS using SmsManager API.

First We make a layout.

Android layout file have a two  EditText (phone no, sms message) and  button to send the SMS message.
File : res/layout/main.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#C2B280" android:orientation="vertical" > <TextView android:id="@+id/textViewPhoneNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Phone Number : " android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/editTextPhoneNo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" > </EditText> <TextView android:id="@+id/textViewSMS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Message : " android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/editTextSMS" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:lines="5" android:gravity="top" /> <Button android:id="@+id/buttonSend" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Send" /></LinearLayout>   



File : SMSActivity.java – Activity to send SMS


package com.bishnu.android;

import android.app.Activity;
import android.os.Bundle;
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 SMSActivity extends Activity {

 Button buttonSend;
 EditText textPhoneNo;
 EditText textSMS;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  buttonSend = (Button) findViewById(R.id.buttonSend);
  textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
  textSMS = (EditText) findViewById(R.id.editTextSMS);

  buttonSend.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    String phoneNo = textPhoneNo.getText().toString();
    String sms = textSMS.getText().toString();

    try {
     SmsManager smsManager = SmsManager.getDefault();
     smsManager.sendTextMessage(phoneNo, null, sms, 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();
    }

   }
  });

 }
}


     Need SEND_SMS permission in AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />

    File : AndroidManifest.xml 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bishnu.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-permission android:name="android.permission.SEND_SMS" />

    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SMSActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


  Open  Two Emulator for testing SMS.
  Phone Number- 5558(Another Emulator Number).




 Download Sample Code from this link
 Click This link then open new Tab and after click on Left-Top File option and click on Download.