Android 判断网络
程序员文章站
2022-06-05 21:08:05
...
/**
- 判断是否有网
- @return true:有网
-
fasle:无网;
-
在清单文件添加读取网络状态的权限;
*/
public static boolean isConnection(Context context) {
if(context!=null){
// 连接管理类,
ConnectivityManager connMgr =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null) {
return activeInfo.isAvailable ();
}
}
return false;
}
//吐司
boolean connection = NetWorkUtils.isConnection (getActivity ( ));
if (connection){
new MyasynTask (list,myAdapter).execute (urlString);
}else{
Toast.makeText (getActivity (),“请检查网络”,Toast.LENGTH_SHORT).show ();
}
return view;
上一篇: 简单的网络判断