很多android程序在打开时,都需要检测网络是否连接,或者GPS是否可用:
1.网络是否连接(包括Wifi和移动网络)
// 是否有可用网络
private boolean isNetworkConnected() {
ConnectivityManager cm =
(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = cm.getActiveNetworkInfo();
if (network != null) {
return network.isAvailable();
}
return false;
}2.wifi是否可用
// Wifi是否可用
private boolean isWifiEnable() {
WifiManager wifiManager = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
return wifiManager.isWifiEnabled();
}3.GPS是否可用
// Gps是否可用
private boolean isGpsEnable() {
LocationManager locationManager =
((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE));
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
本文详细介绍了Android程序中如何检测网络是否连接、WiFi是否可用以及GPS是否可用的方法,涵盖了网络状态、WiFi状态和GPS状态的判断逻辑。
4389

被折叠的 条评论
为什么被折叠?



