Android webview覆盖链接

时间:2017-10-02 13:55:51

标签: android url webview override

我试图覆盖Android网站中的注销链接,以重定向到简介活动,而不是重定向到网站的退出页面。

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //opens links in webview
        view.loadUrl(url);

        if(url.endsWith("logout")) {
            Intent intent = new Intent(SignIn.this, Menu.class);
            startActivity(intent);
        }
        return true;
    }

注意:当然,Menu.class是我的第一个活动(我要重定向的地方)和SignIn.this是webview所在的当前活动。

我正在尝试几种方式,这就是我现在所拥有的。有什么建议?

提前感谢您的考虑

1 个答案:

答案 0 :(得分:0)

您可以点击“退出”按钮

来调用javascript方法

前:

<script type="text/javascript">
    function showAndroidLogout() {
        Android.showAndroidLogout();
    }
</script>`

在Android Activity Signin中你可以添加一个包含你方法实现的JavascriptInterface

ex:`public class WebAppInterface {     上下文mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
}

/** Show Menu activity from the web page */
@JavascriptInterface
public void showLogout() {
     Intent intent = new Intent(SignIn.this, Menu.class);
        startActivity(intent);
}

}`

相关问题