How to prevent choosePrivateKeyAlias dialog in android app?

前端 未结 2 1115
春和景丽
春和景丽 2021-01-14 08:36

I have an android app that call a secured website in a webview. The webview retrieve the certificate to give it to the website.

I have to use the KeyChain.choos

2条回答
  •  萌比男神i
    2021-01-14 09:22

    I'm not sure about the best way to resolve this problem, but here is what I did that worked fine for me.

    I checked a boolean variable in the preferences, and if it returns false, I display the choosePrivateKeyAlias window. If it returns true, I know that I have permission to retrieve the certificate directly, so there's no need to display the popup.

    boolean isGranted = prefs.getBoolean("MY_CERT", false);
    if(!isGranted) {
            //Get cert and private key from internal android store
            KeyChainAliasCallback keyChainAliasCallback = new KeyChainAliasCallback() {
                @Override
                public void alias(@Nullable String s) {
                    Log.d(TAG, "selected alias = " + s);
                    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                    editor.putBoolean("MY_CERT", true);
                    editor.commit();
                    retriveCertsTask.execute();
                }
            };
            KeyChain.choosePrivateKeyAlias(mActivity, keyChainAliasCallback, null, null, null, -1, CERT_ALIAS);
        } else {
            // Retrieve certs an private key
            retriveCertsTask.execute();
        }
    }
    

    Hope it helps...

提交回复
热议问题