Android工具类

下面的工具类来显示开发中经常用到的一些方法:慢慢的增加;

/**
 * 类名: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;
	}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	/**
<span style="white-space:pre">	</span> * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的
<span style="white-space:pre">	</span> * @param context
<span style="white-space:pre">	</span> * @return true 表示开启
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static final boolean isOPen(final Context context) {
<span style="white-space:pre">		</span>LocationManager locationManager 
<span style="white-space:pre">		</span>                         = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
<span style="white-space:pre">		</span>// 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)
<span style="white-space:pre">		</span>boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
<span style="white-space:pre">		</span>// 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)
<span style="white-space:pre">		</span>boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
<span style="white-space:pre">		</span>if (gps || network) {
<span style="white-space:pre">			</span>return true;
<span style="white-space:pre">		</span>}


<span style="white-space:pre">		</span>return false;
<span style="white-space:pre">	</span>}</span>

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值