Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

open ads in external browser in webview android

I created app with webview, and i want load all internal links in webview and load external links in android browser. Now problem is I am using html ads and when i click on ads i want open external browser, but its opening in webview. only problem with ads otherwise everything is works fine. So how can i do this?

My code is below:

`class MyWebViewClient extends WebViewClient {

@Override   
public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("www.mysite.com")) {
    view.loadUrl(url);
    return true;
}else{

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;}}`
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You code should be:

@Override   
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("www.mysite.com")) {
        return true;
    }else{
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return false;
    }
}

All I changed was:

1.) Returning true loads the URL in the webview, no need for view.loadUrl()

2.) Return false when you broadcast the ACTION_VIEW intent


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...