如果使用WebChromeClient轻触market://链接,则打开Play商店

时间:2018-09-21 20:55:05

标签: java android webview webchromeclient

如果用户点击以下类型的链接,我想打开Play商店:market://
我尝试使用getUrl(),但它仅是第一次获取URL,而不是在用户点击其他链接时才获取在webView中。
这是我的代码:

    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
    swipeLayout.setOnRefreshListener(this);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setMax(100);
    webview = (WebView) findViewById(R.id.dashboard);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webview.loadUrl("http://example.test");
    webview.setWebViewClient(new WebViewClient());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    progressBar.setProgress(0);
    webview.setWebChromeClient(new WebChromeClient());

    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {

            progressBar.setProgress(progress);

            if (progress == 100) {
                progressBar.setVisibility(View.GONE);
            } else {
                progressBar.setVisibility(View.VISIBLE);
            }
            super.onProgressChanged(view, progress);
        }
    });

1 个答案:

答案 0 :(得分:0)

我让它像这样工作:
创建了新的子类:

    private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getScheme().equals("market")) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                Activity host = (Activity) view.getContext();
                host.startActivity(intent);
                return true;
            } catch (ActivityNotFoundException e) {
                Uri uri = Uri.parse(url);
                view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
                return false;
            }
        }
        return false;
    }
}

还在webview中添加了子类: webview.setWebViewClient(new HelloWebViewClient());