와챠의 우당탕탕 코딩 일기장
[Android/Kotlin] 지문인식 만들기 본문
반응형
갑자기 지문인식 궁금해져서 만들어봄
너무나 간단...
결과 화면은 아래와 같은데.... 지문인식 다이얼로그..는 보안 정책상 캡쳐가 안된다해서 못찍음...
1. Manifest에 권한 추가
<!-- 지문 인식 권한 -->
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
2. dependency 추가
implementation 'androidx.biometric:biometric:1.2.0-alpha04'
3. xml 만들기
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="지문인식을 해주세요."
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toTopOf="@+id/btn_fingerprint"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btn_fingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="지문 인증하기"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4. kt 코드 짜기
package com.example.myfingerprint
import androidx.biometric.BiometricPrompt
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.example.myfingerprint.databinding.ActivityMainBinding
import java.util.concurrent.Executor
private lateinit var binding: ActivityMainBinding
private lateinit var executor: Executor
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
executor = ContextCompat.getMainExecutor(this@MainActivity)
biometricPrompt = BiometricPrompt(this@MainActivity, executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int,
errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Toast.makeText(applicationContext, "에러", Toast.LENGTH_SHORT).show()
binding.textview.text = "에러 발생!"
}
override fun onAuthenticationSucceeded(
result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Toast.makeText(applicationContext, "성공", Toast.LENGTH_SHORT).show()
binding.textview.text = "지문인식에 성공하였습니다!"
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Toast.makeText(applicationContext, "실패", Toast.LENGTH_SHORT).show()
binding.textview.text = "지문인식에 실패하였습니다..."
}
})
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("지문 인증")
.setSubtitle("기기에 등록된 지문을 이용하여 지문을 인증해주세요.")
.setNegativeButtonText("취소")
.build()
// 버튼 클릭 시 지문인식
binding.btnFingerprint.setOnClickListener{
binding.textview.text = "지문인식 중..."
biometricPrompt.authenticate(promptInfo)
}
}
}
참고
공식문서: https://developer.android.com/training/sign-in/biometric-auth?hl=ko
반응형
'코딩 일기장 > Android(Kotlin)' 카테고리의 다른 글
[Android/Kotlin]CustomView: color 속성 추가하기 (0) | 2022.04.26 |
---|---|
[Android/Kotlin]음성인식(STT:Speech-To-Text) 만들기 (0) | 2022.04.21 |
[Android/Kotlin]Custom View 만들기/Spinner처럼 생긴 TextView 만들기/ClickListener 추가 (0) | 2022.03.20 |
[Android/Kotlin]EditText scollbar custom/cursor color (0) | 2022.03.19 |
[Android/Kotlin] TabLayout: unselected tab indicator/미선택된 탭의 이터레이터 색 설정/탭 밑줄/tab underline (0) | 2022.03.19 |
Comments