【问题标题】:WebViewClient loads previous user data even after logoutWebViewClient 即使在注销后也会加载以前的用户数据
【发布时间】:2014-07-18 09:27:54
【问题描述】:

我正在使用以下方法将 URL 加载到我的 WebView 中:

mWebView.setWebviewClient(new WebViewClient);

此 URL 加载 Http 身份验证页面。用户登录后,其凭据将保存在 SharedPreference 中。此外,我还实现了清除所有数据的注销功能。

当我再次尝试加载 url 时,它没有显示登录页面,它会自动使用以前的凭据登录。如果我在普通浏览器中加载 URL,登录页面加载正常。只有当我应用 WebViewClient 时,我才会得到上述行为。

这是我的代码:-

myWebView.getSettings().setJavaScriptEnabled(true);

        myWebView.loadUrl(url);
        WebViewDatabase.getInstance(Details1.this).clearHttpAuthUsernamePassword();
        WebViewDatabase.getInstance(Details1.this).clearUsernamePassword();
        WebViewDatabase.getInstance(Details1.this).clearFormData();


        myWebView.setWebViewClient(new WebViewClient())

我很困惑,已经在网上搜索了答案,但找不到任何东西。

【问题讨论】:

    标签: android webview webviewclient


    【解决方案1】:

    我花了太多时间在这上面。如https://code.google.com/p/android/issues/detail?id=22272 所述,这些方法都不起作用。

    最后我通过在onReceivedHttpAuthRequest中设置标志wasLoggegIn解决了这个问题,然后当我想清除登录凭据时,检查标志:

    if (wasLoggedIn) {
      Intent i=new Intent(this,MainActivity.class);
      i.setAction(Intent.ACTION_MAIN);
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
      finish();
      try {startActivity(i);}
      catch (java.lang.Exception ignore) {}
      System.exit(0);
    }
    

    是的,重新启动应用程序。这是唯一的方法,至少在 4.4.2 上是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 2015-08-24
      • 2018-06-08
      相关资源
      最近更新 更多