Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. package com.facticoapp.supercivicos.services;
  2.  
  3. import android.app.Activity;
  4. import android.app.Service;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.location.Location;
  8. import android.os.IBinder;
  9. import android.support.v4.content.LocalBroadcastManager;
  10. import android.util.Log;
  11.  
  12. import com.facticoapp.supercivicos.dialogues.Dialogues;
  13. import com.facticoapp.supercivicos.location.CustomLocation;
  14. import com.facticoapp.supercivicos.location.LocationClientListener;
  15. import com.facticoapp.supercivicos.preferences.PreferencesManager;
  16.  
  17. /**
  18. * Created by Edgar Z. on 27/01/16.
  19. */
  20. public class LocationService extends Service {
  21. public final String TAG_CLASS = LocationService.class.getName();
  22.  
  23. private LocationClientListener clientListener;
  24.  
  25. public static final String LOCATION = "location";
  26. public static final String LOCATION_LAT = "lat";
  27. public static final String LOCATION_LON = "lon";
  28.  
  29. @Override
  30. public void onCreate() {
  31. super.onCreate();
  32.  
  33. Dialogues.Log(TAG_CLASS, "onCreate", Log.DEBUG);
  34.  
  35. clientListener = new LocationClientListener(getBaseContext());
  36. clientListener.setOnLocationClientListener(onLocationClientListener);
  37. }
  38.  
  39. @Override
  40. public int onStartCommand(Intent intent, int flags, int startId) {
  41. Dialogues.Log(TAG_CLASS, "onStartCommand", Log.DEBUG);
  42.  
  43. if (clientListener != null) clientListener.connect();
  44.  
  45. //return super.onStartCommand(intent, flags, startId);
  46. return Service.START_STICKY;
  47. }
  48.  
  49. private LocationClientListener.OnLocationClientListener onLocationClientListener = new LocationClientListener.OnLocationClientListener() {
  50. @Override
  51. public void onLocationChanged(Location location) {
  52. Dialogues.Log(TAG_CLASS, "Service Latitude: " + location.getLatitude() + ", Longitude" + location.getLongitude(), Log.ERROR);
  53.  
  54. PreferencesManager.putLocationPreference(getApplication(),
  55. String.valueOf(location.getLatitude()),
  56. String.valueOf(location.getLongitude()));
  57.  
  58. sendLocationBroadcast(location);
  59.  
  60. stopSelf();
  61. }
  62. };
  63.  
  64. @Override
  65. public void onDestroy() {
  66. Dialogues.Log(TAG_CLASS, "onDestroy", Log.DEBUG);
  67. clientListener.disconnect();
  68. super.onDestroy();
  69. }
  70.  
  71. @Override
  72. public IBinder onBind(Intent intent) {
  73. return null;
  74. }
  75.  
  76. private void sendLocationBroadcast(Location location) {
  77. Intent intent = new Intent(LocationService.LOCATION);
  78. intent.putExtra(LOCATION_LAT, String.valueOf(location.getLatitude()));
  79. intent.putExtra(LOCATION_LON, String.valueOf(location.getLongitude()));
  80. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement