Webview中如何保存用户名和密码

How to Save Username and Password in Webview

目前,我还在学习Android开发中,如果我的这个问题对您来说不是很容易理解,请原谅。

我创建了一个 Android 应用程序,它使用 RecyclerView 显示一组列表,当用户单击列表中的每个名称时,它会将他们重定向到一组不同的网站,它将是显示在 WebView 中。这些网站中的每一个都有不同的登录页面。我不控制那些网站。这些网站关闭了它们的 cookie。现在,在这种情况下是否可以保存用户的用户名和密码?

当我包含 Facebook、Instagram 和 Twitter 时,我的应用程序能够保存用户名和密码(可能是因为它们正在保存 cookie?)。但是如果我尝试我想添加的特定网站(在特殊情况下),用户名和密码不会被保存。

由于 setSavePassword 方法已被弃用,我找不到其他方法来实现保存用户名和密码。我已经多次阅读有关 SharedPreferences、JavaScript 和 Autofill 的内容,但我不知道如何将它们正确地实现到我的应用程序中。因为从我读到的大部分内容来看,建议它首先需要一个登录页面来保存用户名和密码。

这是我的 WebView 代码。

public class WebViewActivity extends AppCompatActivity {

    private WebView webView;
    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        
        webView = findViewById(R.id.webView);
        
        WebSettings webSettings = webView.getSettings();
        
        websettings.setDomStorageEnabled(true);
        websettings.setJavaScriptEnabled(true);
        webView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
        webView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
        
        if (Build.VERSION.SDK_INT >= 21) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        } else {
            CookieManager.getInstance().setAcceptCookie(true);
        }
        
        webView.loadUrl(url);
        
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        }
        ...
    }
    
    ...
}

谢谢。

public class WebViewActivity extends AppCompatActivity {

    private WebView webView;
    String url = "";

    class MyJavaScriptInterface {
        MyJavaScriptInterface() {
        }
        @SuppressWarnings("unused")
        @JavascriptInterface
        public void processContent(String user,String pass) {
            Log.e("Remember", "user:"+user+" pass:"+pass);
            if (!(user.length() >= 1 && pass.length() >= 1))
                return;

               
            SharedPreferences sharedPreferences = getContext().getSharedPreferences("UserInformation",MODE_PRIVATE);
            if (sharedPreferences.getBoolean("isClickYes", false)) {
                sharedPreferences.edit().putString("us",user).apply();
                sharedPreferences.edit().putString("pass",pass).apply();
            }

            if (!sharedPreferences.getBoolean("isClickYes", false)) {
                AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                        .setTitle("save information")
                        .setMessage("Are You Want Save Password?")

                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                sharedPreferences.edit().putString("us",user).apply();
                                sharedPreferences.edit().putString("pass",pass).apply();
                                sharedPreferences.edit().putBoolean("isClickYes",true).apply();
                            }
                        })

                        .setNegativeButton(android.R.string.no, null)
                        .setIcon(android.R.drawable.ic_popup_reminder)
                        .show();
            }

        }
    }

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

        webView = findViewById(R.id.webView);

        WebSettings webSettings = webView.getSettings();

        websettings.setDomStorageEnabled(true);
        websettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new MyJavaScriptInterface(), "INTERFACE");
        webView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
        webView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");

        if (Build.VERSION.SDK_INT >= 21) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        } else {
            CookieManager.getInstance().setAcceptCookie(true);
        }

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }



private void runOnUi(int page, WebView view ,SharedPreferences sharedPreferences) {
    int LOGIN      = 0;
    int LOGIN_PASS = 1;
    WebViewActivity.this.runOnUiThread(() -> {
        if (page == LOGIN_PASS) {
        view.loadUrl("javascript:(function(){l=document.getElementById(\"formLogin\"); l.setAttribute(\"onclick\",\"" +
                "INTERFACE.processContent(  document.getElementById('txtUserId').value , document.getElementById('txtPassword').value  );\"   );       " + "})()");

        if (sharedPreferences.getBoolean("isClickYes", false)) {
            String pass = sharedPreferences.getString("pass", "");
            view.loadUrl("javascript:(function(){l=document.getElementById('txtPassword');l.value='" + pass + "'; })()");
        }
        } else if (page == LOGIN) {
            String user = sharedPreferences.getString("us", "");
            view.loadUrl("javascript:(function(){l=document.getElementById('txtUserId2');l.value='" + user + "'; })()");
        }
    });
}



            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {




    if (request.getUrl().toString().equals("https/.../....png")) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("UserInformation", MODE_PRIVATE);
        if (request.getRequestHeaders().toString().contains("Referer=https/...../loginpass"))
            runOnUi(1, view, sharedPreferences);
        else if (request.getRequestHeaders().toString().contains("Referer=https/...../login"))
            runOnUi(0, view, sharedPreferences);
    }



                return super.shouldInterceptRequest(view, request);
            }
        }
    
    }
    
    webView.loadUrl(url);
    ...
}

写这样的 1000 行代码而不是

webview.getSettings().setSavePassword(true);

webview.loadUrl("url");