本文共 1286 字,大约阅读时间需要 4 分钟。
下面的工具类来显示开发中经常用到的一些方法:慢慢的增加;
/** * 类名:Utils * 功能描述:网络状态检测工具类 * @author android_ls */public class Utils { /** * 检测当的网络(WLAN、3G/2G)状态 * @param context Context * @return true 表示网络可用 */ public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.isConnected()) { // 当前网络是连接的 if (info.getState() == NetworkInfo.State.CONNECTED) { // 当前所连接的网络可用 return true; } } } return false; }
/** * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的 * @param context * @return true 表示开启 */ public static final boolean isOPen(final Context context) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); // 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快) boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位) boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (gps || network) { return true; } return false; }
}
转载地址:http://zoalf.baihongyu.com/