Best solution for my task: Thread, AsyncTask or Service?

84 Views Asked by At

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.

2

There are 2 best solutions below

1
Vishal Chhodwani On BEST ANSWER

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

0
Akshay On

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