【问题标题】:Android webview Override linkAndroid webview 覆盖链接
【发布时间】:2017-10-02 13:55:51
【问题描述】:

我正在尝试覆盖 android 网站中的注销链接,以重定向到 Intro Activity 而不是网站的注销页面。

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 所在的当前活动。

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

提前感谢您的考虑

【问题讨论】:

    标签: android url webview overriding


    【解决方案1】:

    您可以在单击“注销”按钮时调用javascript方法

    例如:

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

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

    例如:`公共类 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);
    }
    

    }`

    【讨论】:

    • 感谢 croenilso,但老实说,您的解决方案过于复杂。将 javascript 注入已经有 javascript 的 webview 会增加冲突风险,减慢解决方案中的所有内容,该解决方案与完全原生应用程序相比已经更慢。我真的只是想让我的 webclientsetting 工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多