Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的自动读取OTP/SMS_Android_Manifest_Android Broadcastreceiver - Fatal编程技术网

android中的自动读取OTP/SMS

android中的自动读取OTP/SMS,android,manifest,android-broadcastreceiver,Android,Manifest,Android Broadcastreceiver,我正在开发一个Android应用程序,其中服务器发送一个OTP,用户需要在应用程序中输入这个OTP,才能注册我的应用程序。我想要的是,我的应用程序应该能够自动读取服务器发送的OTP。我怎样才能做到这一点?在此方面的任何帮助或指导都将不胜感激 谢谢。。!提前您有3个选项可以自动读取OTP短信: 1。使用SMS权限阅读所有传入的SMS: 不再建议,因为这需要用户明确授予SMS权限 2。在Google play服务中使用短信检索器API: 建议。但这需要对OTP SMS格式进行一些服务器级更改

我正在开发一个Android应用程序,其中服务器发送一个OTP,用户需要在应用程序中输入这个OTP,才能注册我的应用程序。我想要的是,我的应用程序应该能够自动读取服务器发送的OTP。我怎样才能做到这一点?在此方面的任何帮助或指导都将不胜感激


谢谢。。!提前

您有3个选项可以自动读取OTP短信:

1。使用SMS权限阅读所有传入的SMS:

不再建议,因为这需要用户明确授予SMS权限

2。在Google play服务中使用短信检索器API:

建议。但这需要对OTP SMS格式进行一些服务器级更改。这只适用于安装了播放服务的设备

3。在SmsManager类中使用createAppSpecificSmsToken(仅限Android O):

不建议这样做,因为到目前为止,这只适用于Android O

使用库

  • 在清单中添加这些权限

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    
  • 在onCreate活动中初始化SmsVerifyCatcher

    smsVerifyCatcher = new SmsVerifyCatcher(getActivity(), new OnSmsCatchListener<String>() {
        @Override
        public void onSmsCatch(String message) {
            String code = parseCode(message);//Parse verification code
            Log.d("Agilanbu OTP", code);
            Toast.makeText(getActivity(), "Agilanbu OTP: " + code, Toast.LENGTH_LONG).show();
            et_otp.setText(code);//set code in edit text
        }
    });
    
  • 解析消息

    private String parseCode(String message) {
     Pattern p = Pattern.compile("\\b\\d{6}\\b");
     Matcher m = p.matcher(message);
     String code = "";
      while (m.find()) {
          code = m.group(0);
      }
     return code;
    }
    


除非您的应用程序是默认短信处理程序,否则Google Play不再允许
接收短信权限

因此,到目前为止,一个可能的解决方案是使用

您需要一个
BroadcastReceiver
和一个执行
SmsRetriever.getClient(context.startSmsRetriever()的任务

在您的接收器中:

if(SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
   Bundle extras = intent.getExtras();
   Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
   final String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
}
“我如何才能做到这一点?”——与服务器的维护人员交谈并询问他们。他们是唯一知道他们的“服务器如何发送OTP”,是否适合您“自动读取服务器发送的OTP”等的人。
private String parseCode(String message) {
 Pattern p = Pattern.compile("\\b\\d{6}\\b");
 Matcher m = p.matcher(message);
 String code = "";
  while (m.find()) {
      code = m.group(0);
  }
 return code;
}
if(SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
   Bundle extras = intent.getExtras();
   Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
   final String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
}