Android SMS App Tutorial

Last modified on March 1st, 2015 by Joe.

In this Android tutorial, I will walk you through to create an Android SMS application. We will be using a BroadcaseReceiver to receive the SMS message and notify the user. To send SMS we will use the SmsManager class of the telephony package.

We have already seen about sending and receiving SMS in separate tutorials. On popular request, we will create a single app that is capable of sending and receiving SMS message. To send SMS messages refer the earlier tutorial’s code snippet. All I have done now is to create a single main activity and branch out to two different activities to send and receive SMS.

Refer the Android receive SMS tutorial for receive part of the code. I have added a compose button to the inbox activity and made some minor styling.

Android-SMS-App

Download

SMS Main Activity

Main SMS activity containst just two buttons. On click of them forwards to the inbox or compose activity.

package com.javapapers.android.androidsmsapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

public class MainActivity extends ActionBarActivity {

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

    public void goToInbox(View view) {
        Intent intent = new Intent(MainActivity.this, ReceiveSmsActivity.class);
        startActivity(intent);
    }

    public void goToCompose(View view) {
        Intent intent = new Intent(MainActivity.this, SendSmsActivity.class);
        startActivity(intent);
    }

}

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="#ffff8d7a">

    <TextView android:text="@string/androidSMS" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="101dp"
        android:capitalize="characters"
        android:textSize="34dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Inbox"
        android:id="@+id/btnInbox"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:onClick="goToInbox" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Compose"
        android:id="@+id/btnCompose"
        android:layout_alignTop="@+id/btnInbox"
        android:layout_alignParentEnd="true"
        android:onClick="goToCompose" />

</RelativeLayout>

Send SMS

Following method is the core part of sending SMS. You may download the complete example App to get the full code.

    protected void sendSMS() {
        String toPhoneNumber = toPhoneNumberET.getText().toString();
        String smsMessage = smsMessageET.getText().toString();
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(toPhoneNumber, null, smsMessage, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent.",
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "Sending SMS failed.",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

Send-SMS

Receive SMS

Core code for parsing the message from the SMS inbox is as follows, please download the project for complete code or refer the earlier tutorial.

   public void refreshSmsInbox() {
        ContentResolver contentResolver = getContentResolver();
        Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
        int indexBody = smsInboxCursor.getColumnIndex("body");
        int indexAddress = smsInboxCursor.getColumnIndex("address");
        long timeMillis = smsInboxCursor.getColumnIndex("date");
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
        String dateText = format.format(date);

        if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
        arrayAdapter.clear();
        do {
            String str = smsInboxCursor.getString(indexAddress) +" at "+
                    "\n" + smsInboxCursor.getString(indexBody) +dateText+ "\n";
            arrayAdapter.add(str);
        } while (smsInboxCursor.moveToNext());
    }

Receive-SMS

Download

Comments on "Android SMS App Tutorial"

  1. OHS says:

    Hello, Thanks for this awesome, straightforward tutorial… I do have a problem that I’d love to be solved though. I want the messages I send from this app to appear in other messaging apps installed on the phone (the default one for example). Currently whatever I send doesn’t show up when I open another messaging app. Can you tell me how to do this or maybe direct me to another tutorial where I can find my answer? Any help is much appreciated. Thanks.

  2. Preetham says:

    Hi Joe,
    Your tutorial was really good. when I run this in my emulator it was showing me that “Unfortunately androidsmsapp has stopped”. How do i need to resolve? can you please help me Joe.

    Thank you.

Comments are closed for "Android SMS App Tutorial".