现在开发的项目中经常用到定位功能,如果每次都去新写一个监听,太过于繁琐和增加代码重复量,于是写一个定位工具类。
首先,定位的基本流程是实例化一个LocationManager对象,同时获得手机支持的定位Provider,即
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> providerList = locationManager.getProviders(true);
常用的定位Provider有GPS:LocationManager.GPS_PROVIDER,和 手机网络的LocationManager.NETWORK_PROVIDER。
然后设置定位监听,locationManager.requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener);
,其中,第一个参数为支持的定位参数,GPS或者NETWORK,第二次参数是多少毫秒间隔获取一次位置信息,第三个参数是变化多少米会更新一次位置信息,
第四个参数是位置监听参数,同时在LocationListener的onProviderEnabled(String provider)接口中,可以获取上一次的位置信息,即
Location mLocation = locationManager.getLastKnownLocation(provider)。在onLocationChanged(Location location)
直接获取的位置Location。
最后,可以在应用程序不需要定位监听是取消定位服务,LocationManager.removeUpdates (LocationListener listener)。
具体的工具类,看完整代码:
public class LocationHelper implements LocationListener { private static LocationHelper instance = null; private LocationManager locationManager = null; private Context mContext = null; private Location mLocation = null; private String provider = null; private boolean isListened = false; private LocationHelper(Context context) { mContext = context; locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); List<String> providerList = locationManager.getProviders(true); if (providerList.contains(LocationManager.GPS_PROVIDER)) { provider = LocationManager.GPS_PROVIDER; } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) { provider = LocationManager.NETWORK_PROVIDER; } else { provider = LocationManager.NETWORK_PROVIDER; } setLocationListener(); } public static LocationHelper getInstance(Context context) { if (instance == null) { instance = new LocationHelper(context); } return instance; } public boolean setLocationListener() { if (!isListened && mContext != null) { if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) { locationManager.requestLocationUpdates(provider, 5000, 10, this); isListened = true; } } return isListened; } public void removeLocationListener() { if (locationManager != null) { if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) { locationManager.removeUpdates(this); } } } @Override public void onLocationChanged(Location location) { Log.d(this.getClass().getSimpleName(), "lat: " + location.getLatitude() + " ,lng: " + location.getLongitude()); mLocation = location; } @Override public void onStatusChanged(String provider, int i, Bundle bundle) { } @Override public void onProviderEnabled(String provider) { if (mContext != null) { if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) { mLocation = locationManager.getLastKnownLocation(provider); Log.d(this.getClass().getSimpleName(), "last location lat: " + mLocation.getLatitude() + " ,lng: " + mLocation.getLongitude()); } } } @Override public void onProviderDisabled(String provider) { } public Location getLocation() { return mLocation; } }