Open link only with . com - Android Webview

Asked

Viewed 317 times

1

In the code below, link with mcpack are open in the Webview application, however, sometimes it opens in the default browser, which I do?

@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url)
{
    if (url.contains("https://www.mediafire.com/file/") || (url.endsWith("mcpack") || (url.contains("https://www.mediafire.com/download_repair.php") || (url.contains("http://engine.addroplet.com/")) ))) { // Could be cleverer and use a regex
        mWebView.loadUrl(url);

        return false;



    }

    mWebView.stopLoading();
    mWebView.goBack();
    final AlertDialog alertDialog = new AlertDialog.Builder(Textures.this).create();
    alertDialog.setTitle("Navegador");
    alertDialog.setMessage("Deseja abrir o navegador?");
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Sim", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);

            }

        });
    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Não", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });


    alertDialog.show();




    return false;
}

1 answer

1


In the documentation Android of the method shouldOverrideUrlLoad says:

Do not call WebView.loadUrl(String) with the request’s URL and then return true. This unnecessarily cancels the Current load and Starts a new load with the same URL. The correct way to continue loading the Given URL is to Simply return false, without Calling WebView.loadUrl(String).

That summing up means: nay call the WebView.loadUrl this will cause another request to begin, just return false.

This could be what’s making your code work in an unstable way. You can take the mWebView.loadUrl(url); and only return false or put as the code below.

Remembering that if you return true he will abort the requisition.

@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url)
{
    if (url.contains("https://www.mediafire.com/file/") || (url.endsWith("mcpack") || (url.contains("https://www.mediafire.com/download_repair.php") || (url.contains("http://engine.addroplet.com/")) ))) { // Could be cleverer and use a regex
        return super.shouldOverrideUrlLoading(view, url)
    }
}

Another detail is that you talk "link with mcpack" whereas in the code you are only checking if it ends with url.endsWith("mcpack"). Then it’s only worth you putting some Log.d in your code to check if you are actually entering this if.

  • Did not solve, continue opening in the browser :-(

  • @Daniel Vale just you put some Log.d in your code to check if you are actually entering this method and this if. Something like Log.d("TESTE-WEBVIEW", "ENTROU NO METODO "+url ) at the beginning of the function and another within the if Log.d("TESTE-WEBVIEW", "ENTROU NO IF"+url ) and see if it appears in Logcat

  • I tried twice, the first time you didn’t download, and the second time you asked to open the browser. ENTROU NO METODO https://www.mediafire.com/file/2t8pfp3o8eo9tm3/Dark_Mode_v1.8.0_r-2.mcpack/file TESTE-WEBVIEW ENTROU NO METODO http://download1638.mediafire.com/x1549vkcyldg/2t8pfp3o8eo9tm3/Dark+Mode+v1.8.0+r.2.mcpack TESTE-WEBVIEW ENTROU NO METODO https://www.mediafire.com/file/2t8pfp3o8eo9tm3/Dark_Mode_v1.8.0_r-2.mcpack/file

  • the if will not work because it does not end with mcpack, you’ll have to use contains instead of endsWith

  • Not really, but when the user click on download will open another link, in it the end contains mcpack.

  • 1

    Thank you very much, it works!

Show 1 more comment

Browser other questions tagged

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