android otp接口,android - OTP (token) should be automatically read from the message - Stack Overflow...

I will recommend you not to use any third party libraries for auto fetch OTP from SMS Inbox.

This can be done easily if you have basic understanding of Broadcast Receiver and how it works.

Just Try following approach :

Step 1) Create single interface i.e SmsListner

package com.wnrcorp.reba;

public interface SmsListener{

public void messageReceived(String messageText);}

Step 2) Create single Broadcast Receiver i.e SmsReceiver

package com.wnrcorp.reba;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver {

private static SmsListener mListener;

Boolean b;

String abcd,xyz;

@Override

public void onReceive(Context context, Intent intent) {

Bundle data = intent.getExtras();

Object[] pdus = (Object[]) data.get("pdus");

for(int i=0;i

SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);

String sender = smsMessage.getDisplayOriginatingAddress();

// b=sender.endsWith("WNRCRP"); //Just to fetch otp sent from WNRCRP

String messageBody = smsMessage.getMessageBody();

abcd=messageBody.replaceAll("[^0-9]",""); // here abcd contains otp

which is in number format

//Pass on the text to our listener.

if(b==true) {

mListener.messageReceived(abcd); // attach value to interface

object

}

else

{

}

}

}

public static void bindListener(SmsListener listener) {

mListener = listener;

}

}

Step 3) Add Listener i.e broadcast receiver in android manifest file

and add permission

Final Step 4) The activity where you going to auto fetch otp when it is received in inbox. In my case I'm fetching otp and setting on edittext field.

public class OtpVerificationActivity extends AppCompatActivity {

EditText ed;

TextView tv;

String otp_generated,contactNo,id1;

GlobalData gd = new GlobalData();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_otp_verification);

ed=(EditText)findViewById(R.id.otp);

tv=(TextView) findViewById(R.id.verify_otp);

/*This is important because this will be called every time you receive

any sms */

SmsReceiver.bindListener(new SmsListener() {

@Override

public void messageReceived(String messageText) {

ed.setText(messageText);

}

});

tv.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

try

{

InputMethodManager imm=

(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);

}

catch(Exception e)

{}

if (ed.getText().toString().equals(otp_generated))

{

Toast.makeText(OtpVerificationActivity.this, "OTP Verified

Successfully !", Toast.LENGTH_SHORT).show();

}

});

}

}

Layout File for OtpVerificationActivity

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_otp_verification"

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.wnrcorp.reba.OtpVerificationActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/firstcard"

xmlns:card_view="http://schemas.android.com/apk/res-auto"

card_view:cardCornerRadius="10dp"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:background="@android:color/white">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="OTP Confirmation"

android:textSize="18sp"

android:textStyle="bold"

android:id="@+id/dialogTitle"

android:layout_margin="5dp"

android:layout_gravity="center"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/otp"

android:layout_margin="5dp"

android:hint="OTP Here"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Verify"

android:textSize="18sp"

android:id="@+id/verify_otp"

android:gravity="center"

android:padding="10dp"

android:layout_gravity="center"

android:visibility="visible"

android:layout_margin="5dp"

android:background="@color/colorPrimary"

android:textColor="#ffffff"

/>

Screenshots for OTP Verification Activity where you fetch OTP as soons as

messages received

Oivp7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值