Open external links in another browser

Asked

Viewed 749 times

1

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

But I would like the external links to be opened in another browser. For example: You click on a Google Adsense ad that you have on the site and the ad site opens in the same App and would like it to go to another one, so any link other than my domain is prompted 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);
}

1 answer

1


I don’t know if there’s any better solution but, all of a sudden, this is the one I can think of.

You can use the method shouldOverrideUrlLoading() to intercept the url and check that it is external to their domain and, if yes, 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;
    }
}
  • I’m gonna take a test.

  • Excuse my ignorance, as I reported: I am inexperienced in java... so the schema there that you did inside the if Uri is red and with the message Cannot resolve to Symbol "".

  • 1

    I think I solved with this: Uri Uri = Uri.parse(url); I’ll test it! Wait

  • Until you solved your answer! Except that besides appearing the browsers the site still opens in the App. More I will consider your answer, helped a lot. I’ll try to sort it out here. Thank you!

  • Look at the changes, now an external link should no longer open on Webview. Pay attention to the Returns and the call to webview.loadUrl(url) is the most,

  • All right! Thank you very much!

  • Ramaral, can you answer that question: http://answall.com/questions/151060/navigator-n%C3%A3o-reload-a-%C3%Baltima-p%C3%A1gina

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.