以编程方式打开 Android 凭据设置

Open Android credentials Setup programmatically

要显示敏感数据,用户可以在我的应用中启用身份验证。我正在使用 the android in-build authentication.

但是,如果用户没有使用任何模式、密码、密码或生物特征验证来保护他的设备,我想打开 android 设置,他可以在其中设置他的验证。有什么意图/方法可以去那里吗?我没找到。

到目前为止的一些代码: 判断用户是否没有设置任何认证方式:

androidx.biometric.BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) activity, executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);

            // Determine, if the user has no device password set.
            boolean errorCodeIsBeingHandledSeparately = false;
    // HERE WE DETERMINE THAT CREDENTIALS HAVE NOT BEEN SETUP
            if (errorCode == BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL) {
                if (authenticationInterface != null) {
                    errorCodeIsBeingHandledSeparately = true;
                    authenticationInterface.onUserHasNoDevicePassWordSet();
                }
            }

            // Display error message, only if the error code is not being handled seperately.
            if (!errorCodeIsBeingHandledSeparately) {
                Toast.makeText(activity, "Authentication error\n" + errString, Toast.LENGTH_LONG).show();
            }


        }

        @Override
        public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);

            if (authenticationInterface == null) {
                Toast.makeText(activity, "Success", Toast.LENGTH_LONG).show();
            }
            else {
                authenticationInterface.onUserSuccessfullyAuthenticated();
            }
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Toast.makeText(activity, "Authentication failed", Toast.LENGTH_LONG).show();
        }
    });

接收认证的接口return。

/**
 * Interface to receive authentication return.
 */
private AuthenticationUtils.AuthenticationInterface authenticationInterface;
public interface AuthenticationInterface {
    public void onUserSuccessfullyAuthenticated();
    public void onUserHasNoDevicePassWordSet();
}

我想引导用户转到设备设置凭据的对话框。

public void displayNoDeviceCredentialsSetDialog() {
    MaterialAlertDialogBuilder noDeviceCredentialsDialog = new MaterialAlertDialogBuilder(activity, R.style.AlertDialogTheme);
    String noDeviceCredentials_goToSettings_dialogMessage = activity.getString(R.string.authentication_noDeviceCredentials_goToSettings_dialogMessage);
    noDeviceCredentialsDialog.setMessage(noDeviceCredentials_goToSettings_dialogMessage);
    noDeviceCredentialsDialog.setPositiveButton(
            R.string.DialogConfirmationOK,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
         // HERE I WOULD LIKE TO OPEN THE ANDROID SETTING WHERE HE CAN SETUP HIS CREDENTIALS
                }
            }
    );
    noDeviceCredentialsDialog.setNegativeButton(
            R.string.DialogConfirmationNegativeAnswerText,
            null
    );
    noDeviceCredentialsDialog.show();
}

我想去这里:

您可以从此处的设置到达那里:

我正在寻找的是这样的东西:在这里我们将用户导航到其他一些 android 设置。

Intent intent2 = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  intent2.setAction(android.provider.Settings.ACTION_APPLICATION_SETTINGS);
  intent2.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, getPackageName());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
  intent2.setAction(android.provider.Settings.ACTION_APPLICATION_SETTINGS);
  intent2.putExtra("app_package", getPackageName());
  intent2.putExtra("app_uid", getApplicationInfo().uid);
} else {
  intent2.setAction(android.provider.Settings.ACTION_APPLICATION_SETTINGS);
  intent2.addCategory(Intent.CATEGORY_DEFAULT);
  intent2.setData(Uri.parse("package:" + getPackageName()));
}
startActivity(intent2);

刚刚弄明白了。

有 3 个可行的选项可供使用:Settings.ACTION_BIOMETRIC_ENROLL、Settings.ACTION_FINGERPRINT_ENROLL 和 Settings.ACTION_SECURITY_SETTINGS。

我使用的最终实现是:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    activity.startActivity(new Intent(Settings.ACTION_BIOMETRIC_ENROLL));
}
else {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        activity.startActivity(new Intent(Settings.ACTION_FINGERPRINT_ENROLL));
    }
    else {
        activity.startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));
    }
}

Settings.ACTION_FINGERPRINT_ENROLL opens this: 选择备用锁屏方法并设置所选方法后,设备会要求您注册指纹。

Settings.ACTION_SECURITY_SETTINGS 打开这个:

由于缺少高于 Android Build“R”的设备,我无法测试 ACTION_BIOMETRIC_ENROLL,但我认为它类似于 ACTION_FINGERPRINT_ENROLL。

如果您想查看有哪些选项可以打开 android 设置。您可以在 Android Studio 中的任何 Settings.XXX(ACTION_SECURITY_SETTINGS、ACTION_FINGERPRINT_ENROLL、...)上使用“CTRL”+“鼠标单击”。 然后你会看到“..\android\platforms\android-31\android.jar!\android\provider\Settings.class”

如果您很难弄清楚哪个 API 版本用“Build.VERSION_CODES.P”描述,您也可以在构建版本(P、O、 ...)。 然后你会看到这个: