admin 管理员组

文章数量: 1184232

现在,我有一个加载Web视图的应用程序,所有点击都保留在该应用程序中。 我想做的是,在应用程序中单击某个链接(例如http://www.google)时,它将打开默认浏览器。 如果有人有任何想法,请告诉我!

我今天必须做同样的事情,我在StackOverflow上找到了一个非常有用的答案,我想在这里分享,以防其他人需要它。

来源(来自sven)

webView.setWebViewClient(new WebViewClient(){

public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {

view.getContext().startActivity(

new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

return true;

} else {

return false;

}

}

});

这个答案对我很有帮助!谢谢!

请注意,如果该网址是相对网址(不以" http://"开头),则它将在应用内打开。为避免这种情况,请始终返回true并使相对的URL链接不起作用。

您应该检查前缀中的其他协议,例如rtsp,https等。如果链接旨在打开媒体,则应将其重定向到设备媒体播放器。如果没有协议前缀,请标识并提供一个。

确切,我在搜索什么。谢谢

嘿,谢谢你。我有一个问题,当我从启动的活动返回时,具有webview的调用将结束吗?如何确保包含Webview的活动在向后导航时保持打开状态?编辑:nvm,我将noHistory设置为true,谢谢!

删除它没有帮助。

问题是,当我在运行时使用loadURL更改URL时,将触发shouldOverrideUrlLoading函数/方法。但是我们不希望这样,我们只希望单击可以从外部打开,因此需要检查一下是否被触摸

请注意,API 24中不推荐使用shouldOverrideUrlLoading(WebView view, String url)。请检查此答案。

救了我。谢谢你

WebView webview = (WebView) findViewById(R.id.webview);

webview.loadUrl(https://whatoplay/);

您不必包含此代码

// webview.setWebViewClient(new WebViewClient());

相反,您需要在下面使用d代码

webview.setWebViewClient(new WebViewClient()

{

public boolean shouldOverrideUrlLoading(WebView view, String url)

{

String url2="https://whatoplay/";

// all links  with in ur site will be open inside the webview

//links that start ur domain example(http://www.example/)

if (url != null && url.startsWith(url2)){

return false;

}

// all links that points outside the site will be open in a normal android browser

else

{

view.getContext().startActivity(

new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

return true;

}

}

});

您可以为此使用Intent:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));

startActivity(browserIntent);

您应该使用Intent.ACTION_VIEW

您只需要添加以下行

yourWebViewName.setWebViewClient(new WebViewClient());

检查此以获得官方文档。

您可以为此使用Intent:

Uri uriUrl = Uri.parse("http://www.google/");

Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);

startActivity(launchBrowser);

本文标签: 浏览器 单击 链接 Android WebView