Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
786 views
in Technique[技术] by (71.8m points)

Android - Open target _blank links in WebView with external browser

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...