繁体   English   中英

我希望它在应用程序而不是Webview中打开url

[英]I want it to open url in app, not webview

我在这里搜索,但是几乎所有问题都是相反的。 我有一个适用于android studio的webview应用程序。 它将通过我的Webview应用程序打开HTML页面中的所有URL。

但我想添加一些例外。 例如,我想要默认Google Play应用中的https://play.google.com ....而不是我的Webview应用。

摘要:webview应用程序必须通过应用程序本身打开一些常规URL,但通过本机另一个应用程序打开一些例外URL ...

我的webviewclient代码是这样;

public class MyAppWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().endsWith("http://play.google.com")) {

            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

由于在文档中阐明在这里

如果您实际上需要功能完善的Web浏览器,则可能要使用URL目的来调用Browser应用程序,而不是使用WebView来显示它。

例如:

Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

关于您的Google Play特定问题,您可以在此处找到具体方法: 如何直接从我的Android应用程序打开Goog​​le Play商店?

EDITS


可以拦截来自WebView链接点击并实施您自己的操作。 这个答案中得出

WebView yourWebView; // initialize it as always...
// this is the funny part:
yourWebView.setWebViewClient(yourWebClient);

// somewhere on your code...
WebViewClient yourWebClient = new WebViewClient(){
    // you tell the webclient you want to catch when a url is about to load
    @Override
    public boolean shouldOverrideUrlLoading(WebView  view, String  url){
        return true;
    }
    // here you execute an action when the URL you want is about to load
    @Override
    public void onLoadResource(WebView  view, String  url){
        if( url.equals("http://cnn.com") ){
            // do whatever you want
        }
    }
}

shouldOverrideUrlLoading返回false表示当前的WebView处理URL。 因此,必须更改您的if语句:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("play.google.com")) {
        // if the host is play.google.com, do not load the url to webView. Let it open with its app
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);

        return true;
    }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM