지문인식 구현하기

1. AndroidManifest.xml 권한추가

2. gradle dependency추가

3. java코드 추가


1. AndroidManifest.xml 권한추가

<!-- 지문인식 권한부여 -->
<uses-permission android:name="android.permission.USE_BIOMETRIC"/>

 

2. gradle dependency추가

implementation 'androidx.biometric:biometric:1.0.1'

 

3. java코드 추가

public class MainActivity extends AppCompatActivity {

    private Executor executor;
    private BiometricPrompt biometricPrompt;
    private BiometricPrompt.PromptInfo promptInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        executor = ContextCompat.getMainExecutor(this);
        biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(getApplicationContext(), "인증에러!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAuthenticationSucceeded(
                    @NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                Toast.makeText(getApplicationContext(), "인증성공!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                Toast.makeText(getApplicationContext(), "인증실패!", Toast.LENGTH_SHORT).show();
            }
        });

        promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle("지문 인증")
                .setNegativeButtonText("취소")
                .setDeviceCredentialAllowed(false)
                .build();
    }
}

지문인식을하기 위한 기본 설정은 완료했습니다.

 

마지막으로 실행시키기 위해서 아래코드를 원하는 동작에 입력해주시면 됩니다.

biometricPrompt.authenticate(promptInfo);

취소하기 위해서는 cancelAuthentication()을 사용하시면 됩니다.

biometricPrompt.cancelAuthentication();

 


+ Recent posts