Last updated on January 5th, 2020 at 09:36 pm

SMS verification is very easy and more user friendly than email verification. Now a days where most of app have a registration form, we need to confirm the user information is accurate and user enter valid phone no. In this i explain you how we can create simple sms verification in android application.

Step 1.  Before we start create application we need a standard msg to read a verification from message eg.

Your application VPN is : 12345

Step 2. Now create new android project in your Android Studio/ Eclipse.

Step 3. Open your activity_main.xml file inside res -> layout.

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <EditText
        android:id="@+id/gd"
        android:textSize="22dp"
        android:hint="----"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Step 4. Now open your MainActivity.java and add this code.

package com.readlastsms;
 
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class MainActivity extends ActionBarActivity {
 
    TextView txt;
    EditText text;
 
    Handler handler = new Handler();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        txt = (TextView) findViewById(R.id.msg);
        text = (EditText) findViewById(R.id.gd);
 
        handler.postDelayed(run, 1000);
    }
 
    Runnable run = new Runnable() {
        @Override
        public void run() {
            getCode();
        }
    };
 
    public void getCode() {
        StringBuilder smsBuilder = new StringBuilder();
        final String SMS_URI_INBOX = "content://sms/inbox";
        final String SMS_URI_ALL = "content://sms/";
        try {
            Uri uri = Uri.parse(SMS_URI_INBOX);
            String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
            Cursor cur = getContentResolver().query(uri, projection, "address LIKE '%MyTsky'", null, "date desc limit 1");
            if (cur.moveToFirst()) {
                int index_Address = cur.getColumnIndex("address");
                int index_Person = cur.getColumnIndex("person");
                int index_Body = cur.getColumnIndex("body");
                int index_Date = cur.getColumnIndex("date");
                int index_Type = cur.getColumnIndex("type");
                do {
                    String strAddress = cur.getString(index_Address);
                    int intPerson = cur.getInt(index_Person);
                    String strbody = cur.getString(index_Body);
                    long longDate = cur.getLong(index_Date);
                    int int_Type = cur.getInt(index_Type);
 
                    smsBuilder.append("[ ");
                    smsBuilder.append(strAddress + ", ");
                    smsBuilder.append(intPerson + ", ");
                    smsBuilder.append(strbody + ", ");
                    smsBuilder.append(longDate + ", ");
                    smsBuilder.append(int_Type);
                    smsBuilder.append(" ]\n\n");
 
                    String str = strbody;
                    Pattern pattern = Pattern.compile("\\w+([0-9])");
                    Matcher matcher = pattern.matcher(str);
                    for (int i = 0; i < matcher.groupCount(); i++) {
                        matcher.find();
                        Log.e("No....--", matcher.group());
                        if (matcher.group().equals("")) {
                            handler.postDelayed(run, 7000);
                        } else {
                            text.setText(matcher.group());
                        }
                    }
                } while (cur.moveToNext());
 
                if (!cur.isClosed()) {
                    cur.close();
                    cur = null;
                }
            } else {
                smsBuilder.append("no result!");
            } // end if
 
            Log.e("sms", smsBuilder.toString());
            txt.setText(smsBuilder.toString());
        } catch (SQLiteException ex) {
            Log.d("SQLiteException", ex.getMessage());
        }
    }
}

Here in our mainactivty.java  i create a method getCode() in this method i query default database of android to read message for specific no. To read about getContentResolver().query() follow this link.

 String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
            Cursor cur = getContentResolver().query(uri, projection, "address LIKE '%MyTsky'", null, "date desc limit 1");

I read message for specific no to get OTP code.

public void getCode() {
        StringBuilder smsBuilder = new StringBuilder();
        final String SMS_URI_INBOX = "content://sms/inbox";
        final String SMS_URI_ALL = "content://sms/";
        try {
            Uri uri = Uri.parse(SMS_URI_INBOX);
            String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
            Cursor cur = getContentResolver().query(uri, projection, "address LIKE '%MyTsky'", null, "date desc limit 1");
            if (cur.moveToFirst()) {
                int index_Address = cur.getColumnIndex("address");
                int index_Person = cur.getColumnIndex("person");
                int index_Body = cur.getColumnIndex("body");
                int index_Date = cur.getColumnIndex("date");
                int index_Type = cur.getColumnIndex("type");
                do {
                    String strAddress = cur.getString(index_Address);
                    int intPerson = cur.getInt(index_Person);
                    String strbody = cur.getString(index_Body);
                    long longDate = cur.getLong(index_Date);
                    int int_Type = cur.getInt(index_Type);
 
                    smsBuilder.append("[ ");
                    smsBuilder.append(strAddress + ", ");
                    smsBuilder.append(intPerson + ", ");
                    smsBuilder.append(strbody + ", ");
                    smsBuilder.append(longDate + ", ");
                    smsBuilder.append(int_Type);
                    smsBuilder.append(" ]\n\n");
 
                    String str = strbody;
                    Pattern pattern = Pattern.compile("\\w+([0-9])");
                    Matcher matcher = pattern.matcher(str);
                    for (int i = 0; i < matcher.groupCount(); i++) {
                        matcher.find();
                        Log.e("No....--", matcher.group());
                        if (matcher.group().equals("")) {
                            handler.postDelayed(run, 7000);
                        } else {
                            text.setText(matcher.group());
                        }
                    }
                } while (cur.moveToNext());
 
                if (!cur.isClosed()) {
                    cur.close();
                    cur = null;
                }
            } else {
                smsBuilder.append("no result!");
            } // end if
 
            Log.e("sms", smsBuilder.toString());
            txt.setText(smsBuilder.toString());
        } catch (SQLiteException ex) {
            Log.d("SQLiteException", ex.getMessage());
        }
    }

Here i also use handler to check if new message is arrive and read OTP code from the message. This is a simple code to extract OTP from message

String str = strbody;
                    Pattern pattern = Pattern.compile("\\w+([0-9])");
                    Matcher matcher = pattern.matcher(str);
                    for (int i = 0; i < matcher.groupCount(); i++) {
                        matcher.find();
                        Log.e("No....--", matcher.group());
                        if (matcher.group().equals("")) {
                            handler.postDelayed(run, 7000);
                        } else {
                            text.setText(matcher.group());
                        }
                    }

Hope this help you some where. Thanku! 🙂