android webview _blank,Android - Open target _blank links in WebView with

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I build a WebView which displays a website. The website contains links without a target="_blank" attribute and some with it.

I need to open the links with target defined in the external standard device browser and the ones without it inside the WebView.

I'm using a WebViewClient and I tried around with this function but still all my links are opened within the WebView:

Alternative 1:

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

return super.shouldOverrideUrlLoading(view, url);

}

Alternative 2:

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);

return true;

}

Does anybody know how I can open blank-links externally?

Thanks!

PS: To avoid missunderstandings: I can't use this approach because the only way I know the link should be opened externallly is the target attribute.

回答1:

After visiting the above links, I come up with this code and hope this helps.

wv.getSettings().setSupportMultipleWindows(true);

wv.setWebChromeClient(new WebChromeClient() {

@Override

public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)

{

WebView.HitTestResult result = view.getHitTestResult();

String data = result.getExtra();

Context context = view.getContext();

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));

context.startActivity(browserIntent);

return false;

}

});

回答2:

I faced same problem. I wanted to open my web sites pages inside the application and rest all the pages should be open in Default Browser. I used one technique. If URL contains my website name, then I opened it in WebView and rest all the websites opened in Default browser.

Find Below code, I hope It would be useful for all who faced such problems.

private class MyBrowser extends WebViewClient {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url.contains("/internetgeeks")) {

browser.loadUrl(url);

return false;

} else {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse(url));

startActivity(intent);

return true;

}

}

}

回答3:

I also have the same problem and now I've found the solution.

You also need to use WebChromeClient.You can see this and this .

And you can set a WebViewClient to the new WebView and override the shouldOverrideUrlLoading method, then you can get the url and do whatever you want here. If you don't set the WebViewClient, I think it should works too. In my case I want to get the url so I set a WebViewClient to the new WebView.

By the way, if you remove the old webview then when you come back form the browser,the webview is blank. So I retained the webview and added a new one but set the visibility to "gone".

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值