如何使用 switch to enable/disable the Lock 创建指纹锁
How to create Fingerprint Lock using switch to enable/disable the Lock
我想将指纹锁集成到我的应用程序中。用户可以选择 enable/disable 使用开关锁定。我怎样才能以编程方式实现它。
例如:
Choice to enable/disable the lock inside the application
When user opens the application
所以我已经在我的应用程序中实现了这种类型的功能,但我不知道它是否可行您可能会找到一种简单明智的方法,但这就是我所做的
我为此使用了共享首选项,所以首先在存在 https://i.stack.imgur.com/3FY78.jpg 的 activity 中,我喜欢下面的
首先我创建了这个方法
private void Biometric(){
androidx.biometric.BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
// this means we can use biometric sensor
case androidx.biometric.BiometricManager.BIOMETRIC_SUCCESS:
SharedPreferences sharedPreferences = getSharedPreferences("Authentication",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, "1");
editor.putBoolean(SWITCH1, Swicth_authenticate.isChecked());
editor.apply();
Intent intent = new Intent(AboutActivity.this, edit_profile.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
break;
// this means that the device doesn't have fingerprint sensor
case androidx.biometric.BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080101 Authentication failed there's no Fingerprint Reader in your device.", Toast.LENGTH_SHORT).show();
break;
// this means that biometric sensor is not available
case androidx.biometric.BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080102 Authentication failed biometric system not found.", Toast.LENGTH_SHORT).show();
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080103 There's no password for this device.", Toast.LENGTH_SHORT).show();
break;
case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
break;
case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
break;
case BiometricManager.BIOMETRIC_STATUS_UNKNOWN:
break;
}
}
并在用户打开复选框时激活上述方法
如果用户停用我 运行 方法中的以下内容
SharedPreferences sharedPreferences = getSharedPreferences("Authentication",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, "0");
editor.putBoolean(SWITCH1, Swicth_authenticate.isChecked());
editor.apply();
如您所见,我在该方法和我的启动器中使用了共享首选项activity我做了以下事情。
SharedPreferences sharedPreferences = getSharedPreferences("Authentication", 0);
String bio = sharedPreferences.getString(TEXT, "");
if (bio.equals("1")) {
BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK | DEVICE_CREDENTIAL)) {
// this means we can use biometric sensor
case BiometricManager.BIOMETRIC_SUCCESS:
break;
// this means that the device doesn't have fingerprint sensor
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
break;
// this means that biometric sensor is not available
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
break;
case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
break;
case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
break;
case BiometricManager.BIOMETRIC_STATUS_UNKNOWN:
break;
}
// creating a variable for our Executor
Executor executor = ContextCompat.getMainExecutor(this);
// this will give us result of AUTHENTICATION
final BiometricPrompt biometricPrompt = new BiometricPrompt(StartActivity.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();
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
});
// creating a variable for our promptInfo
// BIOMETRIC DIALOG
final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder().setTitle("Authentication")
.setDescription("Use your fingerprint to login ")
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK | DEVICE_CREDENTIAL).build();
biometricPrompt.authenticate(promptInfo);
} else {
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
如果您对下面的评论有任何疑问,我知道它非常庞大,但我不知道您知道多少,所以即使您有疑问,我也几乎编写了整个代码,您可以查看以下源代码 https://github.com/MohammedAbidNafi/MessengerByMargs
我想将指纹锁集成到我的应用程序中。用户可以选择 enable/disable 使用开关锁定。我怎样才能以编程方式实现它。
例如:
Choice to enable/disable the lock inside the application
When user opens the application
所以我已经在我的应用程序中实现了这种类型的功能,但我不知道它是否可行您可能会找到一种简单明智的方法,但这就是我所做的
我为此使用了共享首选项,所以首先在存在 https://i.stack.imgur.com/3FY78.jpg 的 activity 中,我喜欢下面的
首先我创建了这个方法
private void Biometric(){
androidx.biometric.BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
// this means we can use biometric sensor
case androidx.biometric.BiometricManager.BIOMETRIC_SUCCESS:
SharedPreferences sharedPreferences = getSharedPreferences("Authentication",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, "1");
editor.putBoolean(SWITCH1, Swicth_authenticate.isChecked());
editor.apply();
Intent intent = new Intent(AboutActivity.this, edit_profile.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
break;
// this means that the device doesn't have fingerprint sensor
case androidx.biometric.BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080101 Authentication failed there's no Fingerprint Reader in your device.", Toast.LENGTH_SHORT).show();
break;
// this means that biometric sensor is not available
case androidx.biometric.BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080102 Authentication failed biometric system not found.", Toast.LENGTH_SHORT).show();
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080103 There's no password for this device.", Toast.LENGTH_SHORT).show();
break;
case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
break;
case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
break;
case BiometricManager.BIOMETRIC_STATUS_UNKNOWN:
break;
}
}
并在用户打开复选框时激活上述方法 如果用户停用我 运行 方法中的以下内容
SharedPreferences sharedPreferences = getSharedPreferences("Authentication",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, "0");
editor.putBoolean(SWITCH1, Swicth_authenticate.isChecked());
editor.apply();
如您所见,我在该方法和我的启动器中使用了共享首选项activity我做了以下事情。
SharedPreferences sharedPreferences = getSharedPreferences("Authentication", 0);
String bio = sharedPreferences.getString(TEXT, "");
if (bio.equals("1")) {
BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK | DEVICE_CREDENTIAL)) {
// this means we can use biometric sensor
case BiometricManager.BIOMETRIC_SUCCESS:
break;
// this means that the device doesn't have fingerprint sensor
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
break;
// this means that biometric sensor is not available
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
break;
case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
break;
case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
break;
case BiometricManager.BIOMETRIC_STATUS_UNKNOWN:
break;
}
// creating a variable for our Executor
Executor executor = ContextCompat.getMainExecutor(this);
// this will give us result of AUTHENTICATION
final BiometricPrompt biometricPrompt = new BiometricPrompt(StartActivity.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();
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
});
// creating a variable for our promptInfo
// BIOMETRIC DIALOG
final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder().setTitle("Authentication")
.setDescription("Use your fingerprint to login ")
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK | DEVICE_CREDENTIAL).build();
biometricPrompt.authenticate(promptInfo);
} else {
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
如果您对下面的评论有任何疑问,我知道它非常庞大,但我不知道您知道多少,所以即使您有疑问,我也几乎编写了整个代码,您可以查看以下源代码 https://github.com/MohammedAbidNafi/MessengerByMargs