如何在android中的webview中重定向外部链接

时间:2017-07-06 13:12:06

标签: android google-chrome webview google-chrome-webview

我遇到了webview的问题。我在onCreate方法中得到了url。在该页面中,我点击提交按钮后有一个登录屏幕,用户应该重定向另一个不同的网站网址,并进入新选项卡。我怎样才能做到这一点?它现在无法执行任何事情。

@Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

        if (getArguments() != null) {
            tvScreenTitle.setText(getArguments().getString(CONTENT_TITLE));
        }
        new Handler().post(new Runnable() {
            @Override
            public void run() {

                int logoMarginY = (int) (rlScreenHeader.getHeight() * 0.6f);
                int logoMarginX = (int) (rlScreenHeader.getWidth() * 0.3f);

                UIHelper.setMarginTop(btnBack, logoMarginY - btnBack.getHeight() / 2);
                UIHelper.setMarginTop(tvScreenTitle, logoMarginY - tvScreenTitle.getHeight() / 2);
                UIHelper.setMarginLeft(tvScreenTitle, logoMarginX);
            }
        });

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportMultipleWindows(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg)
            {
                webView.loadUrl(url);
                return true;
            }
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
            }
        });
        webView.setWebViewClient(webViewClient);


        webView.loadUrl(this.url);
    }

这是我的onCreateView;

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_app_browser, container, false);
        ButterKnife.bind(this, view);

        UIHelper.setTypeface(rootFragment, GlobalApplication.getInstance().getTypeface());
        return view;
    }

    boolean loadingFinished = true;
    boolean redirect = false;

    WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!loadingFinished) {
                redirect = true;
            }
            loadingFinished = false;
            webView.loadUrl(url);
            return true;  
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            loadingFinished = false;

            AppBrowserFragment.this.url = url;
            tvAddressBarTitle.setText(url);

            progressAddressBar.setVisibility(View.VISIBLE);
            rlAddressBar.setVisibility(View.VISIBLE);
            btnRefresh.setImageResource(R.drawable.browsericon_reloadover);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if(!redirect){
                loadingFinished = true;
            }

            if(loadingFinished && !redirect){
                AppBrowserFragment.this.url = url;

                if (StringUtils.isNotNullOrWhitespace(title)) {
                    tvAddressBarTitle.setText(title);
                }
                else {
                    tvAddressBarTitle.setText(view.getTitle());
                }

                progressAddressBar.setVisibility(View.INVISIBLE);
                rlAddressBar.setVisibility(View.GONE);

                btnRefresh.setImageResource(R.drawable.browsericon_reload);

                btnBackBrowser.setImageResource(webView.canGoBack() ? R.drawable.browsericon_back : R.drawable.browsericon_backover);
                btnForward.setImageResource(webView.canGoForward() ? R.drawable.browsericon_forward : R.drawable.browsericon_forwardover);

            }
            else {
                redirect = false;
            }
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.cancel();
        }
    };

2 个答案:

答案 0 :(得分:0)

使用条件反之亦然

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.contains("/shuffle")) {
        mWebView.loadUrl(url);
        return false;
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

答案 1 :(得分:0)

当用户单击“提交”按钮时,您需要启动新的 intent ,并且必须使用意图中可用的putExtra方法将新URL作为参数传递给intent。

Intent intent = new Intent(this, CLASSNAME.class);
intent.putExtra(URL, <url>);
startActivity(intent);