WebView does not open the requested link

1

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

  

#

I put this code in 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>

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

  

The problem is that when I click the App that is a simple browser that works with WebView, it does not open the link that was clicked but the homepage (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);
}
    
asked by anonymous 28.07.2016 / 22:44

1 answer

1

You need to get the url from the link that was clicked.

This information is passed in the Intent that launched your application. If Action is Intent.ACTION_VIEW then url can be obtained through the getData() method.

In the onCreate() method:

......
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());
.....
    
28.07.2016 / 23:21