前言
Android 开发中经常会遇到获取位置信息的需求,查阅了相关资料,封装了 LocationHelper 方便以后使用
步骤
1、定义 LocationListener,为了和系统的 LocationListener 区分,此处取名: MyLocationListener
import android.location.GpsStatus;
import android.location.Location;
import android.os.Bundle;
/**
* LocationListener
* Created by Jaycee on 2017/3/27.
*/
public interface MyLocationListener {
void updateLastLocation(Location location);
void updateLocation(Location location);// 位置信息发生改变
void updateStatus(String provider, int status, Bundle extras);// 位置状态发生改变
void updateGpsStatus(GpsStatus gpsStatus);// GPS状态发生改变
}
2、封装 LocationHelper
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
/**
* LocationHelper
* Created by Jaycee on 2017/3/27.
*/
public class LocationHelper implements LocationListener {
private static final String TAG = "LocationHelper";
private LocationManager mLocationManager;
private MyLocationListener mListener;
private Context mContext;
public LocationHelper(Context context) {
mContext = context.getApplicationContext();
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
public void initLocation(MyLocationListener listener) {
mListener = listener;
Location location;
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d(TAG, "LocationManager.GPS_PROVIDER");
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
mListener.updateLastLocation(location);
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.d(TAG, "LocationManager.NETWORK_PROVIDER");
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
mListener.updateLastLocation(location);
}
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
public void removeLocationUpdatesListener() {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mLocationManager != null) {
mLocationManager.removeUpdates(this);
}
}
// 定位坐标发生改变
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged: ");
if (mListener != null) {
mListener.updateLocation(location);
}
}
// 定位服务状态改变
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged: ");
if (mListener != null) {
mListener.updateStatus(provider, status, extras);
}
}
// 定位服务开启
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled: " + provider);
}
// 定位服务关闭
@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled: " + provider);
}
}
3、Utils
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
/**
* Utils
* Created by jaycee on 2017/3/28.
*/
public class Utils {
public static boolean isLocationPermissionGranted(@NonNull Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
public static boolean isLocationProviderEnabled(@NonNull Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
}
使用
在 Activity 中使用
import android.Manifest;
import android.location.GpsStatus;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
/**
* with Location
* Created by Jaycee on 2017/3/27.
*/
public abstract class BaseLocationActivity extends AppCompatActivity {
private LocationHelper mLocationHelper;
private double mLongitude;
private double mLatitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Utils.isLocationPermissionGranted(this)
&& Utils.isLocationProviderEnabled(this)) {
initLocation();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mLocationHelper != null) {
mLocationHelper.removeLocationUpdatesListener();
}
}
private void initLocation() {
mLocationHelper = new LocationHelper(this);
mLocationHelper.initLocation(new MyLocationListener() {
@Override
public void updateLastLocation(Location location) {
mLongitude = location.getLongitude();
mLatitude = location.getLatitude();
}
@Override
public void updateLocation(Location location) {
mLongitude = location.getLongitude();
mLatitude = location.getLatitude();
}
@Override
public void updateStatus(String provider, int status, Bundle extras) {
}
@Override
public void updateGpsStatus(GpsStatus gpsStatus) {
}
});
}
protected boolean canGetLocation() {
return mLongitude != 0 || mLatitude != 0;
}
protected double getLongitude() {
return mLongitude;
}
protected double getLatitude() {
return mLatitude;
}
}
关于权限
Android 6.0 以上定位权限需要在运行时请求,可使用 RxPermissions
小结
如有问题,欢迎留言指正