如今,我们已经看到大多数Android手机都包含指纹身份验证。而且我们可以在我们的应用程序中实现指纹身份验证,以尽可能确保我们的应用程序的安全。在本文中,我们将研究指纹认证的实现。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,其中将显示指纹图像和登录按钮。单击登录按钮后,我们将应用指纹。如果将相同的指纹添加到“安全性”设置中,那么我们将获得登录成功。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:添加依赖项和JitPack存储库
导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。
implementation ‘androidx.biometric:biometric:1.0.1’
将JitPack存储库添加到您的构建文件中。将其添加到allprojects {}部分中存储库末尾的根build.gradle中。
allprojects {
repositories {
…
maven { url “https://jitpack.io” }
}
}
添加此依赖项后,同步您的项目,现在我们将继续执行它。
步骤3:使用AndroidManifst.xml文件
将以下行添加到您的AndroidManifst.xml文件中。
步骤4:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
XML
Java
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialising msgtext and loginbutton
TextView msgtex = findViewById(R.id.msgtext);
final Button loginbutton = findViewById(R.id.login);
// creating a variable for our BiometricManager
// and lets check if our user can use biometric sensor or not
BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate()) {
// this means we can use biometric sensor
case BiometricManager.BIOMETRIC_SUCCESS:
msgtex.setText("You can use the fingerprint sensor to login");
msgtex.setTextColor(Color.parseColor("#fafafa"));
break;
// this means that the device doesn't have fingerprint sensor
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
msgtex.setText("This device doesnot have a fingerprint sensor");
loginbutton.setVisibility(View.GONE);
break;
// this means that biometric sensor is not available
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
msgtex.setText("The biometric sensor is currently unavailable");
loginbutton.setVisibility(View.GONE);
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
msgtex.setText("Your device doesn't have fingerprint saved,please check your security settings");
loginbutton.setVisibility(View.GONE);
break;
}
// creating a variable for our Executor
Executor executor = ContextCompat.getMainExecutor(this);
// this will give us result of AUTHENTICATION
final BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
// THIS METHOD IS CALLED WHEN AUTHENTICATION IS SUCCESS
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show();
loginbutton.setText("Login Successful");
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
});
// creating a variable for our promptInfo
// BIOMETRIC DIALOG
final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder().setTitle("GFG")
.setDescription("Use your fingerprint to login ").setNegativeButtonText("Cancel").build();
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
biometricPrompt.authenticate(promptInfo);
}
});
}
}
创建一个新的Drawable Resource File并将其命名为背景。以下是background.xml文件的代码。
XML格式
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialising msgtext and loginbutton
TextView msgtex = findViewById(R.id.msgtext);
final Button loginbutton = findViewById(R.id.login);
// creating a variable for our BiometricManager
// and lets check if our user can use biometric sensor or not
BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate()) {
// this means we can use biometric sensor
case BiometricManager.BIOMETRIC_SUCCESS:
msgtex.setText("You can use the fingerprint sensor to login");
msgtex.setTextColor(Color.parseColor("#fafafa"));
break;
// this means that the device doesn't have fingerprint sensor
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
msgtex.setText("This device doesnot have a fingerprint sensor");
loginbutton.setVisibility(View.GONE);
break;
// this means that biometric sensor is not available
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
msgtex.setText("The biometric sensor is currently unavailable");
loginbutton.setVisibility(View.GONE);
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
msgtex.setText("Your device doesn't have fingerprint saved,please check your security settings");
loginbutton.setVisibility(View.GONE);
break;
}
// creating a variable for our Executor
Executor executor = ContextCompat.getMainExecutor(this);
// this will give us result of AUTHENTICATION
final BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
// THIS METHOD IS CALLED WHEN AUTHENTICATION IS SUCCESS
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show();
loginbutton.setText("Login Successful");
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
});
// creating a variable for our promptInfo
// BIOMETRIC DIALOG
final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder().setTitle("GFG")
.setDescription("Use your fingerprint to login ").setNegativeButtonText("Cancel").build();
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
biometricPrompt.authenticate(promptInfo);
}
});
}
}
输出:
GitHub链接: https : //github.com/Anni1123/FingerprintAuthentication