1. 判断是否有网络
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
public static boolean isNetworkAvailable(Context context) {
if (context == null) return false;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = connManager.getActiveNetworkInfo();
return netinfo != null && netinfo.isConnected();
}
2. 判断wifi是否连接
public static boolean isWIFIConnect(Context context) {
if (context == null) return false;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // 获取系统的连接服务
NetworkInfo netinfo = connManager.getActiveNetworkInfo(); // 获取网络的连接情况
return netinfo != null && netinfo.getType() == ConnectivityManager.TYPE_WIFI; // wifi连接
}
ConnectivityManager.TYPE_MOBILE:代表数据网络
网络
最新推荐文章于 2024-06-28 02:13:44 发布