最近公司项目要实现一个定时向后台放送Gps数据,也就是经纬度,网上的Demo很多,基本都是没有封装过的,用起来很不方便,于是我自己动手封装了一个utilis工具,用起来很方便,直接调用,目前我只知道Android5.0以前的版本会出现异常,其他版本都支持
public class LocationUtils {
private static final long REFRESH_TIME =5000L;
private static final float METER_POSITION =0.0f;
private static ILocationListenermLocationListener;
private static LocationListenerlistener =new MyLocationListener();
private static class MyLocationListenerimplements LocationListener {
@Override
public void onLocationChanged(Location location) {//定位改变监听
if (mLocationListener !=null) {
mLocationListener.onSuccessLocation(location);
}
}
@Override
public void onStatusChanged(String provider,int status, Bundle extras) {//定位状态监听
}
@Override
public void onProviderEnabled(String provider) {//定位状态可用监听
}
@Override
public void onProviderDisabled(String provider) {//定位状态不可用监听
}
}
/**
* GPS获取定位方式
*/
public static Location getGPSLocation(@NonNull Context context) {
Location location =null;
LocationManager manager =getLocationManager(context);
//高版本的权限检查
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return null;
}
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {//是否支持GPS定位
//获取最后的GPS定位信息,如果是第一次打开,一般会拿不到定位信息,一般可以请求监听,在有效的时间范围可以获取定位信息
location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
return location;
}
/**
* network获取定位方式
*/
public static Location getNetWorkLocation(Context context) {
Location location =null;
LocationManager manager =getLocationManager(context);
//高版本的权限检查
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return null;
}
if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//是否支持Network定位
//获取最后的network定位信息
location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
/**
* 获取最好的定位方式
*/
public static Location getBestLocation(Context context, Criteria criteria) {
Location location;
LocationManager manager =getLocationManager(context);
if (criteria ==null) {
criteria =new Criteria();
}
String provider = manager.getBestProvider(criteria,true);
if (TextUtils.isEmpty(provider)) {
//如果找不到最适合的定位,使用network定位
location =getNetWorkLocation(context);
}else {
//高版本的权限检查
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return null;
}
//获取最适合的定位方式的最后的定位权限
location = manager.getLastKnownLocation(provider);
}
return location;
}
/**
* 定位监听
*/
public static void addLocationListener(Context context, String provider, ILocationListener locationListener) {
addLocationListener(context, provider,REFRESH_TIME,METER_POSITION, locationListener);
}
/**
* 定位监听
*/
public static void addLocationListener(Context context, String provider,long time,float meter, ILocationListener locationListener) {
if (locationListener !=null) {
mLocationListener = locationListener;
}
if (listener ==null) {
listener =new MyLocationListener();
}
LocationManager manager =getLocationManager(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.requestLocationUpdates(provider, time, meter,listener);
}
/**
* 取消定位监听
*/
public static void unRegisterListener(Context context) {
if (listener !=null) {
LocationManager manager =getLocationManager(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
//移除定位监听
manager.removeUpdates(listener);
}
}
private static LocationManager getLocationManager(@NonNull Context context) {
return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
/**
* 自定义接口
*/
public interface ILocationListener {
void onSuccessLocation(Location location);
}
/**
* 采用最好的方式获取定位信息
* 获取经度
*/
public static double getBestLocationLatitude() {
Criteria c =new Criteria();//Criteria类是设置定位的标准信息(系统会根据你的要求,匹配最适合你的定位供应商),一个定位的辅助信息的类
c.setPowerRequirement(Criteria.POWER_LOW);//设置低耗电
c.setAltitudeRequired(true);//设置需要海拔
c.setBearingAccuracy(Criteria.ACCURACY_COARSE);//设置COARSE精度标准
c.setAccuracy(Criteria.ACCURACY_LOW);//设置低精度
//... Criteria 还有其他属性,就不一一介绍了
Location best = LocationUtils.getBestLocation(MyApplication.getInstance(), c);
return best.getLatitude();
}
/**
* 采用最好的方式获取定位信息
* 获取纬度
*/
public static double getBestLocationLongtitude() {
Criteria c =new Criteria();//Criteria类是设置定位的标准信息(系统会根据你的要求,匹配最适合你的定位供应商),一个定位的辅助信息的类
c.setPowerRequirement(Criteria.POWER_LOW);//设置低耗电
c.setAltitudeRequired(true);//设置需要海拔
c.setBearingAccuracy(Criteria.ACCURACY_COARSE);//设置COARSE精度标准
c.setAccuracy(Criteria.ACCURACY_LOW);//设置低精度
//... Criteria 还有其他属性,就不一一介绍了
Location best = LocationUtils.getBestLocation(MyApplication.getInstance(), c);
return best.getLongitude();
}
}