How to show message if no internet available in my android webview

前端 未结 6 1850
野的像风
野的像风 2020-12-30 17:07

Hi I am working with android webview application.I uses my the url succesfully in my app and it works only if internet connection available .But I want to show some messages

6条回答
  •  春和景丽
    2020-12-30 17:43

    Use Below code:

    boolean internetCheck;
    /*
         * 
         * Method to check Internet connection is available
         */
    
        public static boolean isInternetAvailable(Context context) {
            boolean haveConnectedWifi = false;
            boolean haveConnectedMobile = false;
            boolean connectionavailable = false;
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();
            NetworkInfo informationabtnet = cm.getActiveNetworkInfo();
            for (NetworkInfo ni : netInfo) {
                try {
    
                    if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                        if (ni.isConnected())
                            haveConnectedWifi = true;
                    if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                        if (ni.isConnected())
                            haveConnectedMobile = true;
                    if (informationabtnet.isAvailable()
                            && informationabtnet.isConnected())
                        connectionavailable = true;
    
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("Inside utils catch clause , exception is"
                            + e.toString());
                    e.printStackTrace();
                    /*
                     * haveConnectedWifi = false; haveConnectedMobile = false;
                     * connectionavailable = false;
                     */
                }
            }
            return haveConnectedWifi || haveConnectedMobile;
        }
    

    It return true if network is available otherwise false In the mantifest add below permissions

    
        
    

提交回复
热议问题