您的位置:首页 > 理论基础 > 计算机网络

开启Android程序时检测网络与GPS是否打开

2013-02-22 11:15 405 查看
很多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);
}


转自:http://blog.csdn.net/sky837/article/details/7867601
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ANDROID