android - SMS Retriever API SMS Broadcaster 问题
问题描述
我过去常常SMS Retriever API
得到OTP
,但我面临的问题是它不是SMS
每次都收到。有时会检索 SMS 内容,有时什么也没有发生。
我已经使用Toast
(Broadcaster started)来显示它是否每次都启动但Toast
也不是每次都显示。我无法诊断问题。
广播接收器代码:
public class OTPBroadcastReceiver extends BroadcastReceiver {
private String otp;
private static OTPSMSReceiveListner otpsmsReceiveListner = null;
private final Pattern p = Pattern.compile("(|^)\\d{4}");
public static void injectListner(OTPSMSReceiveListner listner){
otpsmsReceiveListner = listner;
}
@Override
public void onReceive(Context context, Intent intent) {
try {
Toast.makeText(context,"Broadcaster started",Toast.LENGTH_LONG).show();
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch (status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
//Toast.makeText(context,"success",Toast.LENGTH_LONG).show();
// Get SMS message contents
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
if (message != null) {
Matcher m = p.matcher(message);
if (m.find()) {
otp = m.group(0);
}
String token;
try {
token = CommonMethods.getSecurePref("OTP", context);
} catch (Exception ex) {
token = null;
}
if (token == null) {
//Pass on the text to our listener.
otpsmsReceiveListner.onOTPReceived(otp);
}
}
break;
case CommonStatusCodes.TIMEOUT:
Log.d("onReceive", "timed out (5 minutes)");
//Toast.makeText(context,"Timeout",Toast.LENGTH_LONG).show();
otpsmsReceiveListner.onOTPTimeout();
break;
}
}
}
catch (Exception ex){
Toast.makeText(context,ex.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
}
public interface OTPSMSReceiveListner{
void onOTPReceived(String otp);
void onOTPTimeout();
}
}
OTP 类:
SmsRetrieverClient client = SmsRetriever.getClient(mContext);
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
try
{
Log.e("onSuccess","Successfully started retriever");
}
catch (Exception ex)
{
Log.e("onSuccess",ex.getMessage());
}
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("onFailure", "Failed to start retriever");
}
});
OTPBroadcastReceiver.injectListner(new OTPBroadcastReceiver.OTPSMSReceiveListner() {
@Override
public void onOTPReceived(String otp) {
if(otp.length() == 4) {
otpField.setText(otp);
btnVerify.performClick();
}
}
@Override
public void onOTPTimeout() {
Log.e("onOTPTimeout","onOTPTimeout");
}
});
显现:
<receiver
android:name=".helpers.OTPBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>
短信:
<#> your App OTP is:8149 585dyDy8cbh
解决方案
请参阅此答案https://stackoverflow.com/a/55374780/10449332。请在 SmsRetriever addOnSuccessListener 回调中注册 BroadcastReceiver。
推荐阅读
- r - 从变量中的for循环中选择迭代范围的问题
- node.js - 读取正确的目录但输出错误?
- python - 线程中的 tkinter messagebox.showinfo
- flutter - 使用flutter_barcode_scanner包时将flutter屏幕分成两部分
- python - DM 带有公会 ID 的邀请?
- javascript - XML NodeList Java 对象在 Nashorn Javascript 中显示为空
- javascript - 使用javascript将行号添加到表中
- java - 大尺寸数组会产生 StackOverflowError
- networking - 是否可以在 kubernetes 服务中将外部 IP 设置为 192.168.1.X/24?
- javascript - Discord.Js - 未捕获的 DiscordAPIError:无法向该用户发送消息