Updated 15 December 2016
All of us have seen a plenty of applications that use OTP for login process but pretty much of them access your message and fill the OTP in its place by itself and we just loved it because it saves our work and time. We don’t have to put our application in background and then note the OTP and come back or even go to notification bar to know the OTP. So basically this feature is quiet interesting. Today I will show you how you can implement this in your android application.
Firstly and for mostly we have to create a BroadcastReceiver which will process the message for us. So lets create a receiver named ReceiveMessage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.example.first; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsMessage; import com.android.internal.telephony.SmsConstants; public class ReceiveMessage extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle myBundle = intent.getExtras(); Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; String format = myBundle.getString("format"); SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â msgs[i] = SmsMessage.createFromPdu(pdus[i]); Â Â Â Â Â Â Â Â Â Â Â }else { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â msgs[i] = SmsMessage.createFromPdu(pdus[i], format); Â Â Â Â Â Â Â Â Â Â Â } } for (SmsMessage message : msgs) { String sender = message.getDisplayOriginatingAddress(); String order = message.getMessageBody().toString(); try { if (sender .equals("DZ-WEBKUL")) { LoginActivity Sms = new LoginActivity(); Sms.onRecieveOTP(order); } } catch(Exception e){ e.printStackTrace(); } } } } |
As you can see onReceive we have extracted the message, you can add regular expressions to get the exact code your server is sending, and instantialtied our activity to call a method onRecieveOPT(String). So we are implementing our Activity and this method in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.example.first; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class LoginActivity extends Activity { static EditText otp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); otp =(EditText) findViewById(R.id.myOtp); } public void onClickOTP(View v){ // your server request to send a OTP } public void onRecieveOTP(String code){ otp.setText(code); } } |
So we declared our activity as well as method. You can see here I had put the whole text as it is in EditText. But as I said you can apply a regular expression to extract only code. Also be sure to mention your receiver in manifest file with proper permissions.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" /> <application ...> ....// your activities and other tags <receiver android:name=".ReceiveMessage"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application> |
And you are ready to receive your OTP directly in your application.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.