Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. public class ServiceGPS extends Service {
  2.  
  3. String TAG =getClass().getSimpleName();
  4. private LocationManager mLocationManager = null;
  5. private static final int LOCATION_INTERVAL = 1;
  6.  
  7. private class LocationListener implements android.location.LocationListener {
  8. Location mLastLocation;
  9. private LocationListener(String provider) {
  10. // Log.e(TAG, "LocationListener: " + provider);
  11. mLastLocation = new Location(provider);
  12. }
  13.  
  14. @Override
  15. public void onLocationChanged(Location location) {
  16.  
  17. GblVariabel.latitute = String.valueOf(location.getLatitude());
  18. GblVariabel.longitude = String.valueOf(location.getLongitude());
  19. Log.d(TAG, "onLocationChanged: "+GblVariabel.latitute);
  20. Log.d(TAG, "onLocationChanged: "+GblVariabel.longitude);
  21. mLastLocation.set(location);
  22. }
  23.  
  24. @Override
  25. public void onStatusChanged(String provider, int status, Bundle extras) {
  26. // Log.e(TAG, "onStatusChanged: " + provider);
  27. }
  28.  
  29. @Override
  30. public void onProviderEnabled(String provider) {
  31. // Log.e(TAG, "onProviderEnabled: " + provider);
  32. }
  33.  
  34. @Override
  35. public void onProviderDisabled(String provider) {
  36. // Log.e(TAG, "onProviderDisabled: " + provider);
  37. }
  38. }
  39.  
  40. @Nullable
  41. @Override
  42. public IBinder onBind(Intent intent) {
  43. return null;
  44. }
  45. LocationListener[] mLocationListerner = new LocationListener[]{
  46. new LocationListener(LocationManager.GPS_PROVIDER),
  47. new LocationListener(LocationManager.NETWORK_PROVIDER)
  48. };
  49.  
  50. @Override
  51. public int onStartCommand(Intent intent, int flags, int startId) {
  52. // Log.e(TAG, "Service on Start");
  53. super.onStartCommand(intent, flags, startId);
  54. return START_STICKY;
  55. }
  56.  
  57. @Override
  58. public void onCreate() {
  59. // Log.e(TAG, "onCreate: ");
  60. super.onCreate();
  61. initializeLocationManager();
  62. try {
  63. mLocationManager.requestLocationUpdates(
  64. LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, 1, mLocationListerner[1]
  65. );
  66. } catch (java.lang.SecurityException ex) {
  67. Log.e(TAG, "onCreate: ");
  68. } catch (IllegalArgumentException ex) {
  69. Log.e(TAG, "onCreate: " + ex.getMessage());
  70. }
  71. try {
  72. mLocationManager.requestLocationUpdates(
  73. LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, 0, mLocationListerner[0]
  74. );
  75. } catch (java.lang.SecurityException ex) {
  76. Log.e(TAG, "onCreate: " + ex.getMessage());
  77. } catch (IllegalArgumentException ex) {
  78. Log.e(TAG, "onCreate: " + ex.getMessage());
  79. }
  80. }
  81.  
  82.  
  83. @Override
  84. public void onDestroy() {
  85. super.onDestroy();
  86. if (mLocationManager != null) {
  87. for (int i = 0; i < mLocationListerner.length; i++) {
  88. try {
  89. mLocationManager.removeUpdates(mLocationListerner[i]);
  90. } catch (Exception ex) {
  91. Log.e(TAG, "onDestroy: Fail to remove location");
  92. }
  93. }
  94. }
  95. }
  96.  
  97. private void initializeLocationManager() {
  98. // Log.e(TAG, "initializeLocationManager: Service");
  99. if (mLocationManager == null) {
  100. mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement