Tuesday, 18 August 2015

Send SMS Using Android Application

Hello Guys,

Send an SMS using android application is very easy. You can use SMS Gatewany, SMS Manager or SMS Intent according to your application requirement.

Here I give you a sample code of Send SMS using SMS Intent, which is directly connect with your device messages tools.

Sample Code:

Permission Required: 

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

File: SMSActivity.java

package com.sneha.smsdemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SMSActivity extends Activity 
{
Button send;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);

send = (Button)findViewById(R.id.button_send);

send.setOnClickListener(new OnClickListener() 
{
public void onClick(View v)
{
try 
{
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "You can learn step by step android                                                   development with my tutorial. Just visit                                                                                                   http://creativeandroidguide.blogspot.in/ blog."); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

catch (Exception e) 
{
Toast.makeText(getApplicationContext(),"SMS faild, please try again                                                   later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
}

File: activity_sms.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: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=".SMSActivity"
    android:background="@drawable/bg1">

    <Button
        android:id="@+id/button_send"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/send_button1" />

</RelativeLayout>

File: AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="10"/>
    
    <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=".SMSActivity"
            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>

You can download background image and button image from here.

Background Image:






Button Image:







Happy Coding...!!!