简体   繁体   中英

No internet display dialog

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");

            }

        });

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();

        }

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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM