Android Volley - Checking internet state

后端 未结 6 1859
耶瑟儿~
耶瑟儿~ 2020-12-08 08:30

Before I am using Volley, well as usual, I used AsyncTask to check my internet state.

Here is what I did in AsyncTask:

private class NetCheck extends         


        
6条回答
  •  天命终不由人
    2020-12-08 08:49

    Use this code for checking internet state :

    public class Internet {
        private Context context;
    
        public Internet(Context context) {
            this.context = context;
        }
    
        public Boolean Check() {
            ConnectivityManager cn = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo nf = cn.getActiveNetworkInfo();
            if (nf != null && nf.isConnected() == true) {
                return true;
            } else {
                Toast.makeText(context, "No internet connection.!",
                        Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }
    

提交回复
热议问题