Android原生定位服务LocationManager

  @SuppressLint("MissingPermission")

  public class LocationService extends Service {

  private LocationManager lm;

  private MyLocationListener listener;

  private LocationClient mBDLocationClient = null;

  private MyBDLocationListener mBDLocationListener;

  @Override

  public IBinder onBind(Intent intent) {

  return null;

  }

  @Override

  public void onCreate() {

  super.onCreate();

  createNativeLocation();

  createBDLocation();

  }

  /**

  * 第三方百度定位服务

  */

  private void createBDLocation() {

  mBDLocationClient = new LocationClient(UIUtils.getContext());

  mBDLocationListener = new MyBDLocationListener();

  //声明LocationClient类

  mBDLocationClient.registerLocationListener(mBDLocationListener);

  //配置百度定位的选项

  LocationClientOption option = new LocationClientOption();

  option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);

  option.setCoorType("WGS84");

  option.setScanSpan(10000);

  option.setIsNeedAddress(true);

  option.setOpenGps(true);

  option.SetIgnoreCacheException(false);

  option.setWifiCacheTimeOut(5 * 60 * 1000);

  option.setEnableSimulateGps(false);

  mBDLocationClient.setLocOption(option);

  //开启百度定位

  mBDLocationClient.start();

  }

  /**

  * 原生的定位服务

  */

  private void createNativeLocation() {

  lm = (LocationManager) getSystemService(LOCATION_SERVICE);

  listener = new MyLocationListener();

  Criteria criteria = new Criteria();

  criteria.setAccuracy(Criteria.ACCURACY_COARSE);

  criteria.setAltitudeRequired(false);//不要求海拔

  criteria.setBearingRequired(false);//不要求方位

  criteria.setCostAllowed(true);//允许有花费

  criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗

  String provider = lm.getBestProvider(criteria, true);

  YYLogUtils.w("定位的provider:" + provider);

  Location location = lm.getLastKnownLocation(provider);

  YYLogUtils.w("location-" + location);

  if (location != null) {

  //不为空,显示地理位置经纬度

  String longitude = "Longitude:" + location.getLongitude();

  String latitude = "Latitude:" + location.getLatitude();

  YYLogUtils.w("getLastKnownLocation:" + longitude + "-" + latitude);

  stopSelf();

  }

  lm.requestLocationUpdates(provider, 3000, 10, listener);

  }

  class MyLocationListener implements LocationListener {

  // 位置改变时获取经纬度

  @Override

  public void onLocationChanged(Location location) {

  String longitude = "Longitude:" + location.getLongitude();

  String latitude = "Latitude:" + location.getLatitude();

  YYLogUtils.w("onLocationChanged:" + longitude + "-" + latitude);

  stopSelf(); // 获取到经纬度以后,停止该service

  }

  // 状态改变时

  @Override

  public void onStatusChanged(String provider, int status, Bundle extras) {

  YYLogUtils.w("onStatusChanged - provider:" + provider + " status:" + status);

  }

  // 提供者可以使用时

  @Override

  public void onProviderEnabled(String provider) {

  YYLogUtils.w("GPS开启了");

  }

  // 提供者不可以使用时

  @Override

  public void onProviderDisabled(String provider) {

  YYLogUtils.w("GPS关闭了");

  }

  }

  /**

  * 百度定位的监听

  */

  class MyBDLocationListener extends BDAbstractLocationListener {

  @Override

  public void onReceiveLocation(BDLocation location) {

  double latitude = location.getLatitude(); //获取纬度信息

  double longitude = location.getLongitude(); //获取经度信息

  YYLogUtils.w("百度的监听 latitude:" + latitude);

  YYLogUtils.w("百度的监听 longitude:" + longitude);

  YYLogUtils.w("onBaiduLocationChanged:" + longitude + "-" + latitude);

  stopSelf(); // 获取到经纬度以后,停止该service

  }

  }

  @Override

  public void onDestroy() {

  super.onDestroy();

  // 停止所有的定位服务

  lm.removeUpdates(listener);

  mBDLocationClient.stop();

  mBDLocationClient.unregisterLocationListener(mBDLocationListener);

  }

  }