Requirements:
- App must receive GPS coordinates even if is all activities is stopped (in background).
- 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).- App should post coordinates every several seconds (on all activities).
How I can solve this task on android? My approach is:
- Create singleton User with property
Location location
. - In MainActivity create
LocationListener
and viarequestLocationUpdates
in onLocationChanged callback I set User.getInstance().location. - 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) - The same as 3 in MapActivity
- I create Service and inside it post coordinates of User.getInstance().location on server also via Runnable every several seconds
My questions:
- Is this a good approach?
- Is it a good use of a singleton to save coordinates?
- Is
Runnable
a good choice for interval fetching/posting data from /to server? - Does LocationListener, created in MainActivity, work in all activities and services of the app?