Advertisement
Guest User

Untitled

a guest
May 20th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. package ru.chulakov.ris.utils;
  2.  
  3. import android.app.Service;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.location.Location;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10.  
  11. /**
  12. * Service to getting user location.
  13. */
  14. public class GPSTracker extends Service
  15. {
  16. private LocationManager mLocationManager = null;
  17. private static final int LOCATION_INTERVAL = 1000 * 10;
  18. private static final int LOCATION_DISTANCE = 100;
  19.  
  20. public final static int ACCURACY = 25;
  21.  
  22. /** Constant to transmission latitude through broadcast. */
  23. public static final String LATITUDE = "latitude";
  24. /** Constant to transmission longitude through broadcast. */
  25. public static final String LONGITUDE = "longitude";
  26. /** Intent action for work with broadcast. */
  27. public static final String INTENT_ACTION = "GPSLocationUpdates";
  28.  
  29. private boolean isUsedGPS;
  30.  
  31. /**
  32. * Sends location to activity via broadcast.
  33. *
  34. * @param location Location passed to Activity.
  35. */
  36. private void sendMessageToActivity(Location location) {
  37. Intent intent = new Intent(INTENT_ACTION);
  38. Bundle b = new Bundle();
  39. b.putDouble(LATITUDE, location.getLatitude());
  40. b.putDouble(LONGITUDE, location.getLongitude());
  41. intent.putExtras(b);
  42.  
  43. sendBroadcast(intent);
  44. }
  45.  
  46. private class LocationListener implements android.location.LocationListener{
  47. @Override
  48. public void onLocationChanged(Location location) {
  49. sendMessageToActivity(location);
  50. if (isUsedGPS && location.getAccuracy() < ACCURACY)
  51. disableGPS();
  52. }
  53.  
  54. @Override
  55. public void onProviderDisabled(String provider) {}
  56.  
  57. @Override
  58. public void onProviderEnabled(String provider) {}
  59.  
  60. @Override
  61. public void onStatusChanged(String provider, int status, Bundle extras) {}
  62. }
  63.  
  64. final LocationListener mLocationListenerNetwork = new LocationListener();
  65. final LocationListener mLocationListenerGPS = new LocationListener();
  66.  
  67. @Override
  68. public IBinder onBind(Intent arg0) {
  69. return null;
  70. }
  71.  
  72. private static final String TAG = GPSTracker.class.getSimpleName();
  73. @Override
  74. public int onStartCommand(Intent intent, int flags, int startId) {
  75. super.onStartCommand(intent, flags, startId);
  76.  
  77. if (PermissionUtils.appHasAccessFineLocationPermission(getApplicationContext())) {
  78. requestUpdatesIfAvailable(LocationManager.NETWORK_PROVIDER,
  79. LOCATION_INTERVAL,
  80. LOCATION_DISTANCE,
  81. mLocationListenerNetwork);
  82.  
  83. if (isUsedGPS)
  84. requestUpdatesIfAvailable(LocationManager.GPS_PROVIDER,
  85. LOCATION_INTERVAL,
  86. LOCATION_DISTANCE,
  87. mLocationListenerGPS);
  88. }
  89.  
  90. return START_STICKY;
  91. }
  92.  
  93. @Override
  94. public void onCreate() {
  95. isUsedGPS = true;
  96. initializeLocationManager();
  97. }
  98.  
  99. @Override
  100. public void onDestroy() {
  101. super.onDestroy();
  102. //noinspection MissingPermission
  103. if (mLocationManager != null) {
  104. mLocationManager.removeUpdates(mLocationListenerGPS);
  105. mLocationManager.removeUpdates(mLocationListenerNetwork);
  106. }
  107.  
  108. }
  109.  
  110. private void disableGPS() {
  111. isUsedGPS = false;
  112. mLocationManager.removeUpdates(mLocationListenerGPS);
  113. }
  114.  
  115. private void initializeLocationManager() {
  116. if (mLocationManager == null)
  117. mLocationManager = (LocationManager) getApplicationContext()
  118. .getSystemService(Context.LOCATION_SERVICE);
  119. }
  120.  
  121. /**
  122. * Requests location updates if provider is available.
  123. *
  124. * @param provider Location provider.
  125. * @param locationInterval Interval between updates.
  126. * @param locationDistance Distance between updates.
  127. * @param locationListener Callback function.
  128. */
  129. private void requestUpdatesIfAvailable(String provider,
  130. int locationInterval,
  131. int locationDistance,
  132. LocationListener locationListener)
  133. {
  134. if (!mLocationManager.isProviderEnabled(provider))
  135. return;
  136. //noinspection MissingPermission
  137. mLocationManager.requestLocationUpdates(provider,
  138. locationInterval,
  139. locationDistance,
  140. locationListener);
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement