Sunday 28 July 2013

Send sms in Android Programmatically

1 comment
Today we will learn how to send Sms in android Programmatically. It is often required to have the functionality in your application where you want to send some important message or information to other user/receiver. To demonstrate this functionality I have created a simple demo project named Smsdemo. In this project we will be having two text view one for entering number and one for text or message and one send button. On the tap of it, the message will send to number entered number.

Step 1:- create a layout in the project in layout folder, named activity_main.xml
<LinearLayout 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:orientation="vertical" >
    <EditText
        android:id="@+id/EditText_PhoneNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Number"
        android:inputType="phone" >
    </EditText>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="250dp" >
        <EditText
            android:id="@+id/MainActivity_Message"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ems="10"
            android:hint="Enter message"
            android:inputType="textMultiLine" />
    </RelativeLayout>
    <Button
        android:id="@+id/Send_msg_Btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send_Message" />
</LinearLayout>

Step 2:- create a MainActivity.java
package com.example.smsdemo;
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 MainActivity extends Activity implements OnClickListener {
    Button send;
    EditText phone_Number, message;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) findViewById(R.id.Send_msg_Btn);   
        phone_Number = (EditText) findViewById(R.id.EditText_PhoneNumber);
        message = (EditText) findViewById(R.id.MainActivity_Message);
        send.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        String phone_Num = phone_Number.getText().toString();
        String send_msg = message.getText().toString();
        try {
            SmsManager sms = SmsManager.getDefault();  // using android SmsManager            sms.sendTextMessage(phone_Num, null, send_msg, null, null);  // adding number and text
        } catch (Exception e) {
            Toast.makeText(this, "Sms not Send", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}

Step 3:- AndroidManifest
To send the sms in android you need to add one permission mentioned below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smsdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <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.example.smsdemo.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>
Catch Me On: CreateAppFaster.com

1 comment:

  1. With TemplateMonster's social media PowerPoint template your presentation will look professional without having to spend time on design.

    ReplyDelete