0

I have an Android application that creates and edits requisitions. All the data is going to be stored at my server.

A problem is: the app should regulary get information from the server on existence of some records by generating queries. Time interval between queries towards server should be about 30 seconds.

1) Could you please advise, what is the best way to solve this problem? Maybe there is any better option I've missed?

At the moment I am considering the following options to choose:

  • Thread
  • AsyncTask
  • Service

2) For Service, should I use it at the main thread or place it into another?

3) Also, as far as I know, Thread or AsyncTask are recreated with Activity, when user rotates a phone. Maybe I can use Moxy library with AsyncTask instead of worrying about Activity recreation?

Thank you in advance.

Kurowsky
  • 154
  • 1
  • 9

2 Answers2

0

I suggest you to use Service,it can keep running indefinitely in background. and you canals spawn a thread from service.

Akshay
  • 318
  • 1
  • 15
0

If you need task run in background then Service is best option than AsyncTask and if you want to run your task every 30 seconds then you can use Alarm Manager is good for interval task, but Alarm Manager will consumes a lot of battery.

There is another best solution for Scheduled task That is JobSchedular.

AlarmManager

https://stackoverflow.com/a/8801990/6676466

JobSchedular // I Suggest it

Complete Job Scheduler Example

I suggest you read and use JobSchedular

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • It's look like JobSchedular is really not intended for such short intervals. From API 26 (Oreo), setting a smaller period than 15 minutes for periodic jobs will execute at least after 15 minutes. Until Android 5.1.1 - atleast after 60 seconds. – Kurowsky Mar 03 '18 at 11:48