import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
/**
* gps工具类
*/
public class GpsUtil {
/*
获取一次定位信息
*/
public static Location getLocation(Context context) {
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location =manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location !=null) {
return location;
}
}
if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Location location =manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location !=null) {
return location;
}
}
return null;
}
/**
* @param context you know
* @param minTime 间隔多久更新
* @param minSpacing 距离变化多大更新
* @param listener 回调
* @return 成功与否
*/
public static boolean addChangeListen(Context context, int minTime, float minSpacing, LocationListener listener) {
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minSpacing, listener);
return true;
}
if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minSpacing, listener);
return true;
}
return false;
}
/*
* 设置这个可以根据属性,判断是使用gps还是network,该工具类没用到
*/
private static Criteria getCriteria() {
Criteria criteria =new Criteria();
// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
// 设置是否要求速度
criteria.setSpeedRequired(false);
// 设置是否允许运营商收费
criteria.setCostAllowed(false);
// 设置是否需要方位信息
criteria.setBearingRequired(false);
// 设置是否需要海拔信息
criteria.setAltitudeRequired(false);
// 设置对电源的需求
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
public static boolean checkGps(Context context) {
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return manager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
}