Advertisement
Guest User

Untitled

a guest
Sep 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. package com.rnbglocation.location;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.IntentService;
  5. import android.content.Intent;
  6. import android.location.Location;
  7. import android.os.Handler;
  8.  
  9. import androidx.annotation.Nullable;
  10.  
  11. import com.google.android.gms.location.FusedLocationProviderClient;
  12. import com.google.android.gms.location.LocationCallback;
  13. import com.google.android.gms.location.LocationRequest;
  14. import com.google.android.gms.location.LocationResult;
  15. import com.google.android.gms.location.LocationServices;
  16. import com.google.gson.Gson;
  17.  
  18. import java.util.Date;
  19.  
  20. public class LocationBackgroundService extends IntentService {
  21. private FusedLocationProviderClient mFusedLocationClient;
  22. private LocationCallback mLocationCallback;
  23. private Gson mGson;
  24.  
  25. public LocationBackgroundService() {
  26. super(LocationForegroundService.class.getName());
  27. mGson = new Gson();
  28. }
  29.  
  30. @SuppressLint("MissingPermission")
  31. @Override
  32. protected void onHandleIntent(@Nullable Intent intent) {
  33. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
  34. mLocationCallback = createLocationRequestCallback();
  35.  
  36. LocationRequest locationRequest = LocationRequest.create()
  37. .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
  38. .setInterval(0)
  39. .setFastestInterval(0);
  40.  
  41. new Handler(getMainLooper()).post(() -> mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, null));
  42. }
  43.  
  44. private LocationCallback createLocationRequestCallback() {
  45. return new LocationCallback() {
  46. @Override
  47. public void onLocationResult(LocationResult locationResult) {
  48. if (locationResult == null) {
  49. return;
  50. }
  51. for (Location location : locationResult.getLocations()) {
  52. LocationCoordinates locationCoordinates = createCoordinates(location.getLatitude(), location.getLongitude());
  53. broadcastLocationReceived(locationCoordinates);
  54. mFusedLocationClient.removeLocationUpdates(mLocationCallback);
  55. }
  56. }
  57. };
  58. }
  59.  
  60. private LocationCoordinates createCoordinates(double latitude, double longitude) {
  61. return new LocationCoordinates()
  62. .setLatitude(latitude)
  63. .setLongitude(longitude)
  64. .setTimestamp(new Date().getTime());
  65. }
  66.  
  67. private void broadcastLocationReceived(LocationCoordinates locationCoordinates) {
  68. Intent eventIntent = new Intent(LocationForegroundService.LOCATION_EVENT_NAME);
  69. eventIntent.putExtra(LocationForegroundService.LOCATION_EVENT_DATA_NAME, mGson.toJson(locationCoordinates));
  70. getApplicationContext().sendBroadcast(eventIntent);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement