Search code examples
androidcountdowntimerbackground-service

Best way for android background countdown timer


I have data stored in a database using Firebase and I want it to be stored there for x amount of time, let's say 24 hours, and then removed. Is it better to use a background service countdown timer or to store the time in the database and check time difference until it is 24 hours? What would be the optimal way?


Solution

  • Service countdown is the battery drainer and it is not context aware of battery level, there is no use in syncing data on device which is about to die, I would prefer Jobscheduler which will intelligently act as per various environment criteria met like charging/idle.

    Use Firebase JobDispatcher

    In addition to this, there are several other facilities available to help your app schedule work. These include:

    Caution: exponential backoff is enabled on job dispactchers/schedulers

    For 24-hour logic check here