Sunday 20 October 2013

SMS-Android-Example

SMS Android Example


How to Send an SMS using Android Application?

SmsManager class is responsible for sending sms in Android. 
SMS Manager Manages SMS operations such as sending data, text, and pdu SMS messages.
Step 1:        Initiate SmsManager object by calling SmsManager.getDefault();


 SmsManager smanager = SmsManager.getDefault();

Step 2: send sms by calling sendTextMessage(context,src address, message, sending peding intent, receving pendingintent).
 smanager.sendTextMessage(phoneno, null, msg, null, null);

Simple Example:

Create an Android Application project named SMSProject.

step1: Code for layout xml file: 


        
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Phone Number: " />

   
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="phone" >

       
   

   
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Message: " />

   
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine" />

   
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />

step 2 : code for Activity source java file.


package in.androidshivendra.smsproject;



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 SMSprjctActivity extends Activity {
    /** Called when the activity is first created. */
   EditText edtPhone, edtMsg;
   Button btnSend;
   
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

edtPhone = (EditText) findViewById(R.id.editText1);
edtMsg = (EditText) findViewById(R.id.editText2);
btnSend = (Button) findViewById(R.id.button1);
btnSend.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String phoneno = edtPhone.getText().toString();
String msg = edtMsg.getText().toString();
try{
SmsManager smanager = SmsManager.getDefault();
smanager.sendTextMessage(phoneno, null, msg, null, null);
Toast.makeText(SMSprjctActivity.this,"Message Sent", Toast.LENGTH_SHORT).show();
edtPhone.setText("");
edtMsg.setText("");
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Message sending failed", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}

}
  

Step 3: you have to add android.permission.SEND_SMS permission in manifest file by this way.


uses-permission android:name="android.permission.SEND_SMS"

step4: Now your application is ready for run. Just Test it.

For testing purpose you just start emulator and put port no. like 5556 in place of phone no. and message in message box then send. your msg will send to your emulator.

No comments:

Today's Pageviews