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 ) ; } } 
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  { @Override public  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" ) @Override public  void  onCreate ( )  { super . onCreate ( ) ; new  Thread ( ( )  ->  { Looper . prepare ( ) ; LocationHelper . registerLocation ( getApplicationContext ( ) ,  new  LocationHelper. OnLocationChangeListener ( )  { @Override public  void  onLastKnownLocation ( Location  location)  { if  ( location !=  null )  { Log . e ( "TAG" ,  "onLastKnownLocation= "  +  location) ; } } @Override public  void  onLocationChanged ( Location  location)  { if  ( location !=  null )  { Log . e ( "TAG" ,  "onLocationChanged= "  +  location) ; stopSelf ( ) ; } } } ) ; Looper . loop ( ) ; } ) . start ( ) ; } @Nullable @Override public  IBinder  onBind ( Intent  intent)  { return  null ; } @Override public  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  { @Override public  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 ) ) ; } @Override public  void  onCreate ( )  { super . onCreate ( ) ; GDLocationHelper . register ( getApplicationContext ( ) ,  new  GDLocationHelper. OnLocationChangeListener ( )  { @Override public  void  onLastKnownLocation ( AMapLocation  location)  { if  ( location !=  null )  { Log . e ( "TAG" ,  "onLastKnownLocation= "  +  location. toString ( ) ) ; } } @Override public  void  onLocationChanged ( AMapLocation  location)  { if  ( location !=  null )  { Log . e ( "TAG" ,  "onLocationChanged= "  +  location. toString ( ) ) ; if  ( isOnce)  { stopSelf ( ) ; } } } } ) ; } @Override public  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 @Override public  IBinder  onBind ( Intent  intent)  { return  null ; } @Override public  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" ) ;