Webview does not open the requested link

Asked

Viewed 1,265 times

1

I was looking for how to open links related to my domain as I did in my previous question and I got an answer.

How to make only links in my domain open my application?

I put that code on Androidmanifest.xml:

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:host="oSeuDominio.???" />
<data android:host="www.oSeuDominio.???" />
<data android:pathPattern="/.*" />
</intent-filter>

Like when I click on a link related to my site, for example on Whatsapp and my appears in the list that can open that link.

The problem is that when I click the App it is a simple browser that works with Webview, does not open the link that was clicked, but the home page (index.php) of the site.

My Java code:

  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);
        mWebView.loadUrl("http://xxxx.xxx");
        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);
}
  • In this line mWebView.loadUrl("http://xxxx.xxx"); you have the link to your website?

  • That’s right, that’s it! Like when I open the App it first loads this url there.

1 answer

1


You have to get the url of the link that was clicked.

This information is passed on Intent which launched its application.
If the Action that Intent for Intent.ACTION_VIEW then the url can be obtained by the method getData().

In the method onCreate():

......
Uri uri = Uri.parse("http://xxxx.xxx");//Link por defeito

Intent intent = getIntent();
if(intent.getAction() == Intent.ACTION_VIEW){
    uri = intent.getData()
}
.....
.....
mWebView.loadUrl(uri.toString());
.....
  • One problem: the code mWebView.loadUrl(Uri.toString); that toString turned red, and the error is: Cannot resolves to Symbol "toString"

  • toString is a missing method add 2 parentheses: toString()

  • Our face! It went right and I don’t know how to thank you!

  • Voting and accepting the answers that are useful to you:)

  • Without wanting to be an explorer, if I ask one more question that I believe is simple for you who is experienced in the subject. You answer?

  • 1

    You can ask all the questions you want, if I know will answer(me or other). You will only be considered an "explorer" if you ask for "code ready", showing no effort on your part to try to solve.

  • You are here: http://answall.com/questions/143298/abram-links-externos-navigator. If you can help me, I would be grateful

Show 2 more comments

Browser other questions tagged

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