如何为otp添加自动填充建议

问题描述 投票:0回答:5

我在某些应用程序(不记得名字)中看到,在文本框下,会显示一个小弹出窗口,上面写着

自动填写消息中的代码

我想向我的应用程序添加类似的功能。正如文档中所建议的,添加自动填充提示并设置自动填充重要性以实现此行为。我都尝试过,但都没有成功。 我已经尝试过以下

        <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_login"
        android:layout_marginTop="@dimen/x60"
        android:id="@+id/pin"
        app:errorEnabled="true"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_centerHorizontal="true">
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="PIN"
            android:drawablePadding="@dimen/x16"
            android:inputType="number"
            android:maxLength="6"
            android:id="@+id/et_pin"
            android:importantForAutofill="yes"
            android:autofillHints=".AUTOFILL_HINT_SMS_OTP"
            android:drawableStart="@drawable/ic_pin"/>
    </com.google.android.material.textfield.TextInputLayout>

我想在我的应用程序中获得这种类型的东西

android message autofill one-time-password
5个回答
5
投票

1.在xml中的edittext中添加以下内容:

 android:autofillHints="smsOTPCode"
 android:importantForAutofill="yes"

2.前往设置 > Google > 自动填充 > 短信验证码 > 自动填充服务启用自动填充服务。

注意:此解决方案适用于 android 8 及以上版本。


2
投票

有两种方法可以解决这个问题

  1. 短信读取权限(不推荐)

您可以创建一个弹出窗口并在用户触摸它时授予权限。

  1. 短信检索API(推荐)

您可以设置短信检索器API(查看此链接) 并在用户触摸它时创建一个弹出窗口,然后填充文本视图


1
投票

您是否在 Android 设置中启用了自动填充?

  • 首先选择一个自动填充服务,例如谷歌。
  • 设置 -> Google -> 自动填充 -> 短信验证码 -> 切换为启用

请参阅:https://www.techrepublic.com/article/how-to-enable-sms-verification-code-autofill-in-android/


-2
投票

经过大量搜索并失去希望后,我发现这很简单,只需将其设置为自动填充重要即可

编辑文字代码

<EditText
    android:id="@+id/otp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_margin="30dp"
    android:textColor="@color/black"
    android:autofillHints="smsOTPCode"
    android:importantForAutofill="yes"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/status" />

我也在代码中设置了自动填充提示

lateinit var statusView: TextView
lateinit var otpView: TextView
lateinit var acceptView: RadioButton
lateinit var rejectView: RadioButton
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    statusView = findViewById<TextView>(R.id.status)
    otpView = findViewById<TextView>(R.id.otp)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        otpView.setAutofillHints(AUTOFILL_HINT_SMS_OTP)
    }
    acceptView = findViewById<RadioButton>(R.id.accept)
    rejectView = findViewById<RadioButton>(R.id.reject)
    startListeningForSms(this,this)
    Log.e(javaClass.simpleName, AppSignatureHelper(this).appSignatures.toString())
}
fun startListeningSms(
    context: Context,
    lifecycleOwner: LifecycleOwner
) {
    val client = SmsRetriever.getClient(context)

    // Starts SmsRetriever, which waits for ONE matching SMS message until timeout
    // (5 minutes). The matching SMS message will be sent via a Broadcast Intent with
    // action SmsRetriever#SMS_RETRIEVED_ACTION.
    val task: Task<Void> = client.startSmsRetriever()

    // Listen for success/failure of the start Task. If in a background thread, this
    // can be made blocking using Tasks.await(task, [timeout]);
    task.addOnSuccessListener(OnSuccessListener<Void?> {
        // Successfully started retriever, expect broadcast intent
    })

    task.addOnFailureListener(OnFailureListener {
        // Failed to start retriever, inspect Exception for more details
    })
}

我不会分享整个活动代码,我只是分享与自动填充相关的代码。

AppSignatureHelper 可以在这里找到:https://github.com/googlearchive/android-credentials/blob/master/sms-verification/android/app/src/main/java/com/google/samples/smartlock/sms_verify/ AppSignatureHelper.java

并且可以在依赖项中添加自动填充库

implementation "androidx.autofill:autofill:1.1.0"

-4
投票

Burak Dizlek 的解决方案对我不起作用。

相反 - 我成功地测试了它 - 在这里使用 Android SMS Retriever https://developers.google.com/identity/sms-retriever/overview

使用 SMS Retriever API,您可以在 Android 应用程序中自动执行基于短信的用户验证,无需用户手动输入验证码,也不需要任何额外的应用程序权限

编码

  1. 使用 keytool 或 Helper 获取应用程序哈希,请参阅 https://github.com/shishirthedev/sms-verification-api-android/blob/master/app/src/main/java/com/shishirthedev/smsretriverapi/AppSignatureHashHelper .kt

  2. 启动SmsRetrieverClient

    SmsRetrieverClient client = SmsRetriever.getClient(context);
    Task<Void> task = client.startSmsRetriever()
    
  3. 创建等待 SmsRetriever.SMS_RETRIEVED_ACTION 的广播接收器

    public class MySMSBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
     Log.d(TAG, "onReceive");
     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:
                 // Get SMS message contents
                 String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
            case CommonStatusCodes.TIMEOUT: 
                 // handle timeout
    
  4. 在 Activity onResume 中注册此 BR

     intentFilter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);
     mySMSBroadcastReceiver = new MySMSBroadcastReceiver();
    
     protected void onResume() {  
        super.onResume();
        registerReceiver(mySMSBroadcastReceiver, intentFilter, RECEIVER_EXPORTED);
      }
    

那么 一旦您发送短信(您不需要服务器),您的应用程序将在 BR 内收到它,您可以使用它

onOTPReceived: Your code is: 123456                                                                                                
504w8vEgbb8
© www.soinside.com 2019 - 2024. All rights reserved.