Android 系统定位和高德定位
系统定位
工具类
public class LocationUtils {public static final int REQUEST_LOCATION = 0xa1;public static boolean isOpenLocationServer(Context context) {int locationMode = 0;try {locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);} catch (Settings.SettingNotFoundException e) {e.printStackTrace();return false;}return locationMode != Settings.Secure.LOCATION_MODE_OFF;}public static boolean isGPSEnabled(Context context) {LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);return manager.isProviderEnabled(LocationManager.GPS_PROVIDER);}public static boolean isLocationEnabled(Context context) {LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);return manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || manager.isProviderEnabled(LocationManager.GPS_PROVIDER);}public static void openLocation(Activity activity) {activity.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_LOCATION);}public static void openLocation(Fragment fragment) {fragment.startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), REQUEST_LOCATION);}}
封装LocationManager
public class LocationHelper {private static OnLocationChangeListener mOnLocationChangeListener;private static MyLocationListener mLocationListener;private static LocationManager mLocationManager;private static String mProvider;public static void registerLocation(Context context, @Nullable OnLocationChangeListener listener) {mOnLocationChangeListener = listener;mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);mProvider = mLocationManager.getBestProvider(getCriteria(), true);Location lastKnownLocation = mLocationManager.getLastKnownLocation(mProvider);if (listener != null && lastKnownLocation != null) {listener.onLastKnownLocation(lastKnownLocation);}mLocationListener = new MyLocationListener();startLocation();}public static void startLocation() {mLocationManager.requestLocationUpdates(mProvider, 0, 0, mLocationListener);}public static void stopLocation() {mLocationManager.removeUpdates(mLocationListener);}private static Criteria getCriteria() {Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setSpeedRequired(false);criteria.setCostAllowed(false);criteria.setBearingRequired(false);criteria.setAltitudeRequired(false);criteria.setPowerRequirement(Criteria.POWER_LOW);return criteria;}public static void unregisterLocation() {if (mLocationManager != null) {if (mLocationListener != null) {mLocationManager.removeUpdates(mLocationListener);}}mLocationManager = null;mLocationListener = null;mOnLocationChangeListener = null;}public interface OnLocationChangeListener {void onLastKnownLocation(Location location);void onLocationChanged(Location location);}public static class MyLocationListener implements LocationListener {@Overridepublic void onLocationChanged(@NonNull Location location) {if (mOnLocationChangeListener != null) {mOnLocationChangeListener.onLocationChanged(location);}}}
}
public class LocationService extends Service {public static void actionStart(Context context) {context.startService(new Intent(context, LocationService.class));}public static void actionStop(Context context) {context.stopService(new Intent(context, LocationService.class));}@SuppressLint("MissingPermission")@Overridepublic void onCreate() {super.onCreate();new Thread(() -> {Looper.prepare();LocationHelper.registerLocation(getApplicationContext(), new LocationHelper.OnLocationChangeListener() {@Overridepublic void onLastKnownLocation(Location location) {if (location != null) {Log.e("TAG", "onLastKnownLocation= " + location);}}@Overridepublic void onLocationChanged(Location location) {if (location != null) {Log.e("TAG", "onLocationChanged= " + location);stopSelf();}}});Looper.loop();}).start();}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();LocationHelper.unregisterLocation();}
}
使用
LocationService.actionStart(MainActivity.this, "start");
LocationService.actionStart(MainActivity.this, "stop");
LocationService.actionStart(MainActivity.this, "once");
高德定位
封装高德地图
public class GDLocationHelper {private static AMapLocationClient locationClient;private static OnLocationChangeListener mOnLocationChangeListener;private static MyLocationListener mListener;public static void register(Context context, @Nullable OnLocationChangeListener onLocationChangeListener) {mOnLocationChangeListener = onLocationChangeListener;initLocation(context);}private static void initLocation(Context context) {locationClient = new AMapLocationClient(context);AMapLocation lastKnownLocation = locationClient.getLastKnownLocation();if (lastKnownLocation != null && mOnLocationChangeListener != null) {mOnLocationChangeListener.onLastKnownLocation(lastKnownLocation);}mListener = new MyLocationListener();locationClient.setLocationListener(mListener);}public static void startLocation() {locationClient.stopLocation();locationClient.setLocationOption(getDefaultOption(false));locationClient.startLocation();}public static void startOnceLocation() {locationClient.stopLocation();locationClient.setLocationOption(getDefaultOption(true));locationClient.startLocation();}public static void stopLocation() {locationClient.stopLocation();}public static void unregister() {stopLocation();locationClient.onDestroy();locationClient = null;mOnLocationChangeListener = null;mListener = null;}public static AMapLocationClientOption getDefaultOption(boolean isOnce) {AMapLocationClientOption mOption = new AMapLocationClientOption();mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
if (isOnce) {mOption.setOnceLocation(true);mOption.setOnceLocationLatest(true);} else {mOption.setOnceLocation(false);mOption.setInterval(2_000);}mOption.setMockEnable(false);
mOption.setWifiScan(true); mOption.setLocationCacheEnable(true);
return mOption;}public static class MyLocationListener implements AMapLocationListener {@Overridepublic void onLocationChanged(AMapLocation aMapLocation) {if (mOnLocationChangeListener != null) {mOnLocationChangeListener.onLocationChanged(aMapLocation);}}}public interface OnLocationChangeListener {void onLastKnownLocation(AMapLocation location);void onLocationChanged(AMapLocation location);}
}
public class GDLocationService extends Service {private volatile boolean isOnce = false;public static void actionStart(Context context) {actionStart(context, null);}public static void actionStart(Context context, String command) {context.startService(new Intent(context, GDLocationService.class).putExtra("command", command));}public static void actionStop(Context context) {context.stopService(new Intent(context, GDLocationService.class));}@Overridepublic void onCreate() {super.onCreate();GDLocationHelper.register(getApplicationContext(), new GDLocationHelper.OnLocationChangeListener() {@Overridepublic void onLastKnownLocation(AMapLocation location) {if (location != null) {Log.e("TAG", "onLastKnownLocation= " + location.toString());}}@Overridepublic void onLocationChanged(AMapLocation location) {if (location != null) {Log.e("TAG", "onLocationChanged= " + location.toString());if (isOnce) {stopSelf();}}}});}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {String command = intent.getStringExtra("command");switch (command) {case "once":isOnce = true;GDLocationHelper.startOnceLocation();break;case "stop":GDLocationHelper.stopLocation();break;case "start":default:isOnce = false;GDLocationHelper.startLocation();break;}return super.onStartCommand(intent, flags, startId);}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();GDLocationHelper.unregister();Log.e("TAG", "onDestroy");}
}
使用
GDLocationService.actionStart(MainActivity.this, "start");
GDLocationService.actionStart(MainActivity.this, "stop");
GDLocationService.actionStart(MainActivity.this, "once");