0

I make one Application. In the app i get user current location data and update it into server using restful API. I don't know how to update user data every 5 second even app is closed using api?

Darshi Patel
  • 294
  • 3
  • 11
  • 1
    user service and run in background , It will consume lot of battery so I would not recommend it – Manohar Dec 27 '18 at 06:30
  • 1
    or alternative can be JobScheduler or WorkManager – Sachin Rajput Dec 27 '18 at 06:33
  • Possible duplicate of [How to find my current location (latitude + longitude) in every 5 second in Android?](https://stackoverflow.com/questions/39953053/how-to-find-my-current-location-latitude-longitude-in-every-5-second-in-andr) – Bruno Dec 27 '18 at 09:28

2 Answers2

0

Since this is kind of scheduling task, First thing is choosing the scheduler: If you want the updates to be sent at the exact interval (time constrained) you can choose AlarmManager, If you can compromise on time and want to optimize based on battery, Network availability, Device doze etc, You can choose JobScheduler API.

Now coming to the sending of updates, You can start a background service using any of the above Scheduler APIs, and send updates to backend from within the service. BUT,

Note that Starting android oreo, there are limitations to start a background service while app is in background, you will have to show a persistent notification in the notification bar showing the info about the same.

Anyways, JobScheduler would be the best choice since it will service device reboots too. Also you can retry jobs if it failed in the middle by little changes in the return types of the API methods.

UPDATE: If your task is to update the location info during apps lifecycle only, you can also checkout Handlers and TimerTasks.

Sandesh Baliga
  • 579
  • 5
  • 20
  • can you give me any example? please give me – Darshi Patel Dec 27 '18 at 07:34
  • You could check out https://medium.com/google-developers/scheduling-jobs-like-a-pro-with-jobscheduler-286ef8510129 for knowing how to schedule using `JobScheduler` API. And https://android.jlelse.eu/keep-those-background-services-working-when-targeting-android-oreo-sdk-26-cbf6cc2bdb7f to learn background limitations on oreo. – Sandesh Baliga Dec 27 '18 at 07:36
0

make a location service where every time you ask for location in certain time it send to the server. if you close the application it automatically send unless you kill the application and the service is not working background.

jhayjhay
  • 78
  • 8