——网络管家 【若对该知识点有更多想了解的,欢迎私信博主~~】
制作判断网络状态的方法
public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; }使用
findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean a=isNetworkConnected(getApplicationContext()); if (a==true){ Log.e("asd","网络已连接") }else { Log.e("asd","网络已断开") } } });