0

I'm new to Android development and not sure if this should be asked here or in stack exchange.

Let's say for example, I want to build a simple app that is ad hoc. Meaning for that specific purpose only and not long term. I want to save time building/setting up my own authentication and perhaps for user's convenience of remembering another password. Is there a way where my app can utilize the phone's password authentication if it's set-up by the user. It could be pin, password, pattern. Everytime user opens the app, the phone prompts the lock screen, sort of, before I can use it.

duduwe
  • 888
  • 7
  • 16
  • 1
    you mean somethinglike google pay, paytm do, they ask for current phone password or pin or pattern when you open them? – rahat Jan 21 '21 at 14:31
  • @rahat I have not tried Google Pay. But yes, that behaviour. Ask for my phone's password to open the app, so I can view the data inside or use it. – duduwe Jan 21 '21 at 14:38
  • 1
    check out [this](https://stackoverflow.com/questions/29456456/use-screen-lock-in-my-app) – rahat Jan 21 '21 at 14:41
  • Thanks so much @rahat. I have not thoroughly searched for it I guess. But thanks for pointing me to the answer. – duduwe Jan 21 '21 at 14:43

1 Answers1

0

In manifest file

<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 

In your activity, probably you should use a blank view activity for this

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)
        }

Then in on activity result

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
    }
}
rahat
  • 1,840
  • 11
  • 24
  • while i agree with your answer (to a certain extent) OP has listed `pin, password, pattern.` and biometrics doesn't really fall into this category :) – a_local_nobody Jan 21 '21 at 15:31
  • @a_local_nobody, have you been through the answer? Its for P and above, since `createConfirmDeviceCredentialIntent` is deprecated – rahat Jan 21 '21 at 15:35
  • i did go through your answer, but this is just a biometric prompt, isn't it ? will this actually prompt the user for their pin or password or any of the rest ? – a_local_nobody Jan 21 '21 at 15:39