Open external links in another browser

1

So I have an App that my site is all responsive and is working perfectly.

But I would like the external links to be opened in another browser. For example: The user clicks on a Google Adsense ad that has the site and the ad site opens in the same App and would like it to be another, so any link other than my domain is asked to open in another browser.

Meu código Java:

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (isOnline()) {
        Toast.makeText(getApplicationContext(), "Carregando", Toast.LENGTH_SHORT).show();

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

        mWebView = (WebView) findViewById(R.id.webview);
        Uri uri = Uri.parse("http://xxxx.xx");//Link por defeito
        Intent intent = getIntent();
        if(intent.getAction() == Intent.ACTION_VIEW){
            uri = intent.getData();
        }
        mWebView.loadUrl(uri.toString());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setSupportZoom(false);
        mWebView.setWebViewClient(new LinkWebViewClient());
        mWebView.requestFocusFromTouch();
        mWebView.setWebChromeClient(new WebChromeClient());
   }
   else
    [...]
    }
    private class LinkWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url)
    {
        if(isOnline()) {
            Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_SHORT).show();
            webview.loadUrl(url);
            return true;
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Sem conexão", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.conexaofail);
            return false;
        }
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{

    if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
    {
        if (isOnline()) {
            mWebView.goBack();
            return true;
        }
        else
        {
            setContentView(R.layout.conexaofail);
            return false;
        }
    }
    return super.onKeyDown(keyCode, event);
}
    
asked by anonymous 29.07.2016 / 00:01

1 answer

2

I do not know if there will be another better solution, but suddenly, this is the one that occurs to me.

You can use the shouldOverrideUrlLoading() method to intercept url and check if it is external to your domain and, if so, launch a browser

@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
    Uri uri = Uri.parse(url);

    if(isOnline()) {
        Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_SHORT).show();
        if(uri.getHost().equals("exemplo.com"){//Substitua pelo seu domínio
            //webview.loadUrl(url);//Está a mais
            return false;
        }
        else{
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            return true;
        }
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Sem conexão", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.conexaofail);
        return false;
    }
}
    
29.07.2016 / 00:24