How to check internet connection before app starts and while it is running?

后端 未结 5 1429
一整个雨季
一整个雨季 2021-01-26 15:59

I found a lot of answer about this but unable to implement those also. I want to implement this code here but not able to do so.

This code I found on google documentatio

5条回答
  •  无人及你
    2021-01-26 16:08

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) {
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                return true;
            }
        }
        return false;
    }
    

提交回复
热议问题