我正在开发一个 Android 应用程序,其中服务器发送一个 OTP,用户需要在应用程序中输入这个 OTP,以注册我的应用程序。我想要的是,我的应用程序应该能够自动读取服务器发送的 OTP。我怎样才能做到这一点?非常感谢您在这方面提供任何帮助或指导。
最佳答案
我建议您不要使用任何第三方库从 SMS 收件箱中自动获取 OTP。 如果您对广播接收器及其工作原理有基本的了解,这可以很容易地完成。 只需尝试以下方法:
- 创建单一接口(interface),即 SmsListner
package com.wnrcorp.reba;
public interface SmsListener {
public void messageReceived(String messageText);
}
- 创建单个广播接收器,即 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 < pdus.length; 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;
}
}
- 在 Android list 文件中添加监听器,即广播接收器
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
并添加权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
- 当您在收件箱中收到 otp 时,您要在其中自动获取 otp 的 Activity 。就我而言,我正在获取 otp 并在 edittext 字段上进行设置。
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();
}
});
}
}
}
OtpVerificationActivity 的布局文件
<?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: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.support.v7.widget.CardView
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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white">
<TextView
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"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/otp"
android:layout_margin="5dp"
android:hint="OTP Here"
/>
<TextView
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"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
关于android - OTP( token )应自动从消息中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30862162/