Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. public class MyService extends Service {
  2.  
  3. private static final String TAG = "BOOMBOOMTESTGPS";
  4. private LocationManager mLocationManager = null;
  5. private static final int LOCATION_INTERVAL = 1000;
  6. private static final float LOCATION_DISTANCE = 10f;
  7.  
  8. private class LocationListener implements android.location.LocationListener {
  9. Location mLastLocation;
  10.  
  11. public LocationListener(String provider) {
  12. Log.e(TAG, "LocationListener " + provider);
  13. mLastLocation = new Location(provider);
  14.  
  15.  
  16. }
  17.  
  18. @Override
  19. public void onLocationChanged(Location location) {
  20. Log.e(TAG, "onLocationChanged: " + location);
  21.  
  22. setdatatoserver(location);
  23.  
  24. mLastLocation.set(location);
  25.  
  26. }
  27.  
  28. @Override
  29. public void onProviderDisabled(String provider) {
  30. Log.e(TAG, "onProviderDisabled: " + provider);
  31. }
  32.  
  33. @Override
  34. public void onProviderEnabled(String provider) {
  35. Log.e(TAG, "onProviderEnabled: " + provider);
  36. }
  37.  
  38. @Override
  39. public void onStatusChanged(String provider, int status, Bundle extras) {
  40. Log.e(TAG, "onStatusChanged: " + provider);
  41. }
  42. }
  43.  
  44. LocationListener[] mLocationListeners = new LocationListener[]{
  45. new LocationListener(LocationManager.GPS_PROVIDER),
  46. new LocationListener(LocationManager.NETWORK_PROVIDER)
  47. };
  48.  
  49. @Override
  50. public IBinder onBind(Intent arg0) {
  51. return null;
  52. }
  53.  
  54. @Override
  55. public int onStartCommand(Intent intent, int flags, int startId) {
  56. Log.e(TAG, "onStartCommand");
  57. Toast.makeText(this, "service starting...", Toast.LENGTH_SHORT).show();
  58. super.onStartCommand(intent, flags, startId);
  59. return START_STICKY;
  60. }
  61.  
  62. @Override
  63. public void onCreate() {
  64. Log.e(TAG, "onCreate");
  65. initializeLocationManager();
  66. try {
  67. mLocationManager.requestLocationUpdates(
  68. LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
  69. mLocationListeners[1]);
  70. } catch (java.lang.SecurityException ex) {
  71. Log.i(TAG, "fail to request location update, ignore", ex);
  72. } catch (IllegalArgumentException ex) {
  73. Log.d(TAG, "network provider does not exist, " + ex.getMessage());
  74. }
  75. try {
  76. mLocationManager.requestLocationUpdates(
  77. LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
  78. mLocationListeners[0]);
  79. } catch (java.lang.SecurityException ex) {
  80. Log.i(TAG, "fail to request location update, ignore", ex);
  81. } catch (IllegalArgumentException ex) {
  82. Log.d(TAG, "gps provider does not exist " + ex.getMessage());
  83. }
  84. }
  85.  
  86. @Override
  87. public void onDestroy() {
  88. Log.e(TAG, "onDestroy");
  89. super.onDestroy();
  90. if (mLocationManager != null) {
  91. for (int i = 0; i < mLocationListeners.length; i++) {
  92. try {
  93. mLocationManager.removeUpdates(mLocationListeners[i]);
  94. } catch (Exception ex) {
  95. Log.i(TAG, "fail to remove location listners, ignore", ex);
  96. }
  97. }
  98. }
  99. }
  100.  
  101. private void initializeLocationManager() {
  102. Log.e(TAG, "initializeLocationManager");
  103. if (mLocationManager == null) {
  104. mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
  105. }
  106. }
  107.  
  108.  
  109. public void setdatatoserver(Location location) {
  110.  
  111. Toast.makeText(this, "Location is "+location, Toast.LENGTH_SHORT).show();
  112.  
  113. FirebaseDatabase database = FirebaseDatabase.getInstance();
  114. DatabaseReference myRef = database.getReference("locations");
  115.  
  116. myRef.push().setValue("New Location:"+ location);
  117.  
  118. }
  119.  
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement