0

i've got an app, that has 2 types of "no internet error". When you launch the app, it sees if you have internet and shows an error No Internet, and i have in my webviews, a loadURL to a custom page.

I want to hide the webview website, but with the custom page, it take a litle time, and you can see the error+the website.

Is possible to do something to my needs?

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout. fragment_paginainicio, container, false);
        final WebView mWebView = (WebView) view.findViewById(R.id.webView_websitepage);
        mWebView.loadUrl("https://google.pt");
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                mWebView.loadUrl("file:///android_asset/errorpage.html");

            }

        });

PPKez
  • 1
  • 4
  • confusing question. Please clarify what you need and show your codes also. – Pradeep Behera Oct 19 '19 at 05:03
  • I want not to show the `ERR_INTERNET_DISCONNECTED` because it shows the error and only then is redirecting to my "no internet page". – PPKez Oct 19 '19 at 05:29
  • nice..now we need to see your code so that we can suggest what to do where to achieve your goal. Please share your code. – Pradeep Behera Oct 19 '19 at 05:44
  • I've edited the question! – PPKez Oct 19 '19 at 06:00
  • Possible duplicate of [Prevent WebView from displaying "web page not available"](https://stackoverflow.com/questions/6552160/prevent-webview-from-displaying-web-page-not-available) – AnotherGuy Oct 21 '19 at 20:54
  • @AnotherGuy if you have seen the code you already knew that i already have an errorpage.. – PPKez Oct 21 '19 at 20:59
  • I know. I marked it as a possible duplicate, because there are several answers to that questions that discuss your issue with various solutions. – AnotherGuy Oct 21 '19 at 21:04

2 Answers2

0
You have to check if internet is available. Below code will help in that case. If it is not available you can simple make webview visiblity to gone or do not load anything and show alert dialog for no network 

public static boolean isNetworkAvailable(Context mContext) {

                ConnectivityManager connectivityManager
                        = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                return activeNetworkInfo != null && activeNetworkInfo.isConnected();

        }
Aniruddh Ambarkar
  • 1,030
  • 1
  • 11
  • 20
  • Sorry for answering late, the webview visibility gone was perfect in my case, thank you sir! – PPKez Oct 21 '19 at 22:06
0

This No Internet detection is a little bit tricky. I had struggled a little until I learned this:

There may be situations where you are connected to the router or you have your data connection ON but actually you don't receive any packages back. For some reason the internet provider may be down or you didn't pay your bill and they cut you off.

There is a simple sample of how I check if there is Internet or not.

public boolean isInternetStable() {
    try {
        return new getInternetStabilityStatus().execute().get();
    } catch (InterruptedException e) {
        return false;
    } catch (ExecutionException e) {
        return false;
    }
}
public class getInternetStabilityStatus extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
            return !ipAddr.equals("");

        } catch (Exception e) {
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
    }

}