【问题标题】:Is there a way to lock your developed app using the Phone's built-in authentication? How?有没有办法使用手机的内置身份验证来锁定您开发的应用程序?如何?
【发布时间】:2021-01-21 14:26:29
【问题描述】:

我是 Android 开发新手,不确定是否应该在此处或堆栈交换中询问。

例如,我想构建一个简单的临时应用程序。仅出于该特定目的而不是长期的含义。我想节省时间来建立/设置我自己的身份验证,也许是为了方便用户记住另一个密码。如果我的应用程序是由用户设置的,有没有一种方法可以利用手机的密码身份验证。它可以是别针、密码、图案。每次用户打开应用程序时,手机都会提示锁定屏幕,然后我才能使用它。

【问题讨论】:

  • 你的意思是像 google pay、paytm 这样的东西,当你打开它们时,他们会要求提供当前的手机密码或 pin 或图案?
  • @rahat 我没有尝试过 Google Pay。但是,是的,这种行为。要求输入手机密码才能打开应用,以便查看或使用其中的数据。
  • 签出this
  • 非常感谢@rahat。我猜我还没有彻底搜索过。但是感谢您指出我的答案。

标签: android authentication


【解决方案1】:

在清单文件中

<uses-permission android:name="android.permission.USE_BIOMETRIC" /> //this is for SDK_INT >= 28
<uses-feature android:name="android.software.secure_lock_screen"/>//this is for below 

在您的活动中,您可能应该为此使用空白视图活动

val km = getSystemService(KEYGUARD_SERVICE) as KeyguardManager
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {

            val executor = ContextCompat.getMainExecutor(this)
            val biometricPrompt = BiometricPrompt(this, executor,
        object : BiometricPrompt.AuthenticationCallback() {
    override fun onAuthenticationError(errorCode: Int,
            errString: CharSequence) {
        super.onAuthenticationError(errorCode, errString)
        Toast.makeText(applicationContext,
            "Authentication error: $errString", Toast.LENGTH_SHORT)
            .show()
    }

    override fun onAuthenticationSucceeded(
            result: BiometricPrompt.AuthenticationResult) {
        super.onAuthenticationSucceeded(result)
        Toast.makeText(applicationContext,
            "Authentication succeeded!", Toast.LENGTH_SHORT)
            .show()
    }

    override fun onAuthenticationFailed() {
        super.onAuthenticationFailed()
        Toast.makeText(applicationContext, "Authentication failed",
            Toast.LENGTH_SHORT)
            .show()
    }
})

promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .build()

             biometricPrompt.authenticate(promptInfo)
        }else{
            @Suppress("DEPRECATION")
            //Added in API level 21
            //Deprecated in API level 29 as per android doc
            val i = km.createConfirmDeviceCredentialIntent("title", "description")
            startActivityForResult(i, 1000)
        }

然后进入活动结果

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode==1000&&resultCode==Activity.RESULT_OK){
        //verified open next activity
    }
}

【讨论】:

  • 虽然我同意你的回答(在一定程度上)OP 列出了pin, password, pattern. 并且生物识别技术并不真正属于这一类:)
  • @a_local_nobody,你回答过吗?它适用于 P 及以上,因为 createConfirmDeviceCredentialIntent 已弃用
  • 我确实看过你的回答,但这只是一个生物识别提示,不是吗?这实际上会提示用户输入他们的密码或密码或其他任何内容吗?
猜你喜欢
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
相关资源
最近更新 更多