Wednesday 3 April 2013

How to send SMS in android ?

In android you can send SMS by using SmsManager API. Now we have to describe how send SMS in android pragmatically.

  step 1 :- Create new project give name it to SendSMSDemo. Now we need two TextViews (one for enter phone number and one for message) and one Button for handling event of sending SMS.  Your xml looks like this.

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Android Advance SEND SMS "
        android:textSize="23dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Enter Phone Number"
            android:textSize="16dp"
            android:text />

        <EditText
            android:id="@+id/txtnumbers"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:input />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Enter Your Message"
            android:textSize="16dp"
            android:text />

        <EditText
            android:id="@+id/txtnotes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp" />
    </LinearLayout>

    <Button
        android:id="@+id/btnsend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Send Message" />

</LinearLayout>

  Step 2 :- Now corresponding to this you need to handle event of send SMS. Now when button clicked we have to check phone number field is    empty or not. If Phone Number field is empty then we display Toast message to user that enter phone number.


  SendSMSDemoActivity.java

package com.androidadvance.screen;

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 SendSMSDemoActivity extends Activity implements OnClickListener {
    EditText txtnotes, txtnumber;
    Button btnsend;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txtnotes = (EditText) findViewById(R.id.txtnotes);
        txtnumber = (EditText) findViewById(R.id.txtnumbers);

        btnsend = (Button) findViewById(R.id.btnsend);
        btnsend.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnsend:

       Here we have to check for input.

            if (txtnumber.getText().toString().equals(""))
                Toast.makeText(SendSMSDemoActivity.this,
                        "Please enter number..", Toast.LENGTH_LONG).show();
            else {
                try {

             Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method SmsManager.getDefault().

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(txtnumber.getText().toString()
                            .trim(), null, txtnotes.getText().toString(), 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();
                }
            }
            break;

        default:
            break;
        }
    }
}

 Also not forget to give permission in AndroidManifest.xml

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

1 comment:

  1. This is very nice article for users , Hardik You rock , banda bahot aage tak jayega

    Ashok Rathod

    ReplyDelete