判断网络
程序员文章站
2022-03-04 08:49:02
...
定义一个类
public class NetWrokProwor {
/**
* 网络判断
* @param context
* @return
*/
public static boolean getNetWrokProwor(Context context){
ConnectivityManager systemService = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(systemService == null){
}else{
NetworkInfo[] allNetworkInfo = systemService.getAllNetworkInfo();
if(allNetworkInfo != null){
for (int i = 0 ; i< allNetworkInfo.length ; i++){
if(allNetworkInfo[i].getState() == NetworkInfo.State.CONNECTED){
return true;
}
}
}
}
return false;
}
}