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

前端 未结 6 1852
野的像风
野的像风 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:19

    Call this method before opening the webView if this method returns true that means the internet connection is avialable and you can process to the webview otherwise show some Toast or you can show Dialog if this method returns false.

    Edit

    Use this code like in your Main Activity as like this

    if(isNetworkStatusAvialable (getApplicationContext())) {
        Toast.makeText(getApplicationContext(), "internet avialable", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "internet is not avialable", Toast.LENGTH_SHORT).show();
    
    }
    

    Method

    public static boolean isNetworkStatusAvialable (Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) 
        {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if(netInfos != null)
            if(netInfos.isConnected()) 
                return true;
        }
        return false;
    }
    

提交回复
热议问题