1

I searched here but nearly all questions are opposite.. now I ask; I have an webview app for android studio. It opens all URLs located in HTML page via my webview app.

But I want to add some exception. For example, I want https://play.google.com.... in default Google Play app, not my webview app.

summary: webview app must open some normal URLs via app itself, but some exceptional URLs via native another app...

my webviewclient code is so;

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;
    }
}
İsmail
  • 354
  • 1
  • 5
  • 16

2 Answers2

3

As stated in the docs here:

If you actually want a full-blown web browser, then you probably want to invoke the Browser application with a URL Intent rather than show it with a WebView.

For example:

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

As for your Google Play specific question, you can find out how to do that here: How to open the Google Play Store directly from my Android application?

EDITS


It is possible to intercept link clicks from a WebView and implement your own action. Taken from this answer:

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
        }
    }
}
Community
  • 1
  • 1
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • I must use a local HTML page... URLs are located in it... all urls are opened with my webview app... It is good. but I want only one exceptional url... I want only google play links are opened with its app.. not my webview app – İsmail Mar 02 '16 at 21:32
0

Return false at shouldOverrideUrlLoading means the current WebView handles the URL. So your if statement must be changed:

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;
}
Devrim
  • 15,345
  • 4
  • 66
  • 74
  • I used this but same... anything changed... I use loadUrl("file:///android_asset/home.html"); to show a local HTML file... and google play URL is located in it. but when I click it, all URLs are open with webview... I dont want this, I want all links open with webview but except one URL: google play url must open with itself's app.. – İsmail Mar 02 '16 at 21:29