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

来源:互联网 发布:闪电电商和一洋淘宝 编辑:程序博客网 时间:2024/06/09 22:43

很多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);}




原创粉丝点击