Skip to content Skip to sidebar Skip to footer

How Do I Display No Internet Connection Popup And An Html Page When Internet Is Not Available For Webview?

I am using webviews in my app and want to make sure when internet is not available the pop up message displays for no internet connection and also displays an html page in the back

Solution 1:

You can have below code.

publicclassCheckNetwork {


    privatestaticfinalStringTAG= CheckNetwork.class.getSimpleName();



    publicstaticbooleanisInternetAvailable(Context context)
    {
        NetworkInfoinfo= (NetworkInfo) ((ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if (info == null)
        {
             Log.d(TAG,"no internet connection");
             returnfalse;
        }
        else
        {
            if(info.isConnected())
            {
                Log.d(TAG," internet connection available...");
                returntrue;
            }
            else
            {
                Log.d(TAG," internet connection");
                returntrue;
            }

        }
    }
}

Then in Activity you can use

if(CheckNetwork.isInternetAvailable(MainActivity.this))
 {
       // do something
 }

Note: This checks for Network Connectivity. You may be connected to wifi but you may not have internet connection available.

Solution 2:

if(info.isConnected())
                {
                    Log.d(TAG," internet connection available...");
                    returntrue;
                }
                else
                {
                    Log.d(TAG," internet connection");
                    returntrue;
                }

this condition seems to be a little weird .... try this function for internet connectvity ... works fine for me in all cases

publicbooleancheckNetworkState(Context context) {
    ConnectivityManagerconMgr= (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo infos[] = conMgr.getAllNetworkInfo();
    for (NetworkInfo info : infos) {
        if (info.getState() == State.CONNECTED)
            returntrue;
    }
    returnfalse;
}

Post a Comment for "How Do I Display No Internet Connection Popup And An Html Page When Internet Is Not Available For Webview?"