1

Requirements:

  1. App must receive GPS coordinates even if is all activities is stopped (in background).
  2. App should get data from API and use GPS coordinates as parameters(GET http://someserver?lat=xxx&lng=yyy) every several seconds. This need to be only on two activities(MainActivity and MapActivity).
    1. App should post coordinates every several seconds (on all activities).

How I can solve this task on android? My approach is:

  1. Create singleton User with property Location location.
  2. In MainActivity create LocationListener and via requestLocationUpdates in onLocationChanged callback I set User.getInstance().location.
  3. In MainActivity I get data from server like this: service.getData(User.getInstance().location.getLatitude(), User.getInstance().location.getLongitude, callback) every several seconds via Runnable (like this: https://stackoverflow.com/a/6242292/2568343)
  4. The same as 3 in MapActivity
  5. I create Service and inside it post coordinates of User.getInstance().location on server also via Runnable every several seconds

My questions:

  1. Is this a good approach?
  2. Is it a good use of a singleton to save coordinates?
  3. Is Runnable a good choice for interval fetching/posting data from /to server?
  4. Does LocationListener, created in MainActivity, work in all activities and services of the app?
Community
  • 1
  • 1
igor_rb
  • 1,821
  • 1
  • 19
  • 34

0 Answers0