SMS Verification is a great way of registering a customer as its time saving. You just have to type the phone number and a verification message with OTP (One Time Password) is sent to them which is automatically read by the application. Thats why this feature is gaining popularity and most of the application are adding it to their registration process.

In this tutorial you will learn to create this service. After going through this tutorial you will be able to send the SMS to the user phone number and automatically read it as soon as it is received by the user’s mobile. Also you will learn about Broadcasts.

Create a new project. Then in layout file (activity_main.xml) paste the following code

<?xml version="1.0" encoding="utf-8"?>
<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="com.androidsubway.smsverification.MainActivity">

<EditText
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>

In MainActivity.java paste the following code

import android.media.ExifInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

OtpNumber = (EditText) findViewById(R.id.txt);
}

public static void recivedSms(String message){
try{
OtpNumber.setText(message);
}catch(Exception ex){
ex.printStackTrace();
}
}
}

Now create a broadcaste which actuall calls the recivedSMS method of the MainActivity when the message is received on the phone.

IncomingSMS.java

import android.media.ExifInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

OtpNumber = (EditText) findViewById(R.id.txt);
}

public static void recivedSms(String message){
try{
OtpNumber.setText(message);
}catch(Exception ex){
ex.printStackTrace();
}
}
}

Now open your manifest file and paste the following code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidsubway.smsverification">

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


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<receiver android:name=".IncomingSms">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
Advertisements