Friday, 20 September 2013

Developed using Eclipse 4.3.0, Android API Level 18 

This tutorial will teach you how to send an SMS through your android application.

In android, you can send an SMS through your application in two ways:

  1. Using SMS Manager API
  2. Built-in SMS application
Permissions (required in both methods)

<uses-permission android:name="android.permission.SEND_SMS" />
1. Using SMS Manager API  

Download Code

File: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="@string/enter_phone_number_here"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="23dp"
        android:ems="10"
        android:enabled="false"
        android:gravity="top"
        android:hint="@string/enter_message"
        android:includeFontPadding="false"
        android:inputType="text"
        android:minHeight="200dp"
        android:scrollHorizontally="false"
        android:selectAllOnFocus="false" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="23dp"
        android:text="@string/send" />

</RelativeLayout>
activity_main.xml GUI
 
File: MainActivity.java

package com.mavenscientists.sendsmsSMSManagaer;

import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
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 sendText;
 EditText phoneNumber;
 EditText message;
 
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  sendText = (Button) findViewById(R.id.button1);  
  phoneNumber = (EditText) findViewById(R.id.editText1);
  message = (EditText) findViewById(R.id.editText2);
  
  sendText.setOnClickListener(new OnClickListener()
  {
   @Override
   public void onClick(View v) {
    
    Log.d("sendsms", "Send Text Button clicked");
    String number = phoneNumber.getText().toString();
    String text = message.getText().toString();
    
    try
    {
     SmsManager smsManager = SmsManager.getDefault();
     smsManager.sendTextMessage(number, null, text, null, null);
     Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();
    }
    catch (Exception e)
    {
     Toast.makeText(getApplicationContext(), "Message sending failed", Toast.LENGTH_LONG).show();
     e.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.main, menu);
  return true;
 }

}
File: AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mavenscientists.sendsmsSMSManagaer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
Send SMS App Running

 Download Code - SendSMSusingSMSManagerAPI.zip



Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists