-2

This question is vague but I am not sure what to Google for exactly.

But in my app I have a section where you create a list of tasks with various attributes, and a lot of these numbers are summed up and displayed in daily totals visually. But these daily totals should reset every 24 hours (based on the hour of the day the user chooses for the cutoff, e.g. 3 am if someone works late).

Right now: my database can hold all the data by day. Then my daily counters will visually display the numbers by pulling the corresponding data from the database looking for the current day. That's the easy part.

The hard part: I can refresh the counter after the time cutoff if the user rotates the screen or restarts the app because then it'll be looking for items in the database with a new day that won't be found, so everything will be 0 as intended. But what if the user is just staring at the screen as the time cutoff rolls by? How do I make the app refresh the counters once the time hits? What if they're not even using the app at all (either it's minimized in the background or not even active).

Do I need to have some kind of always-running loop in the background that checks the current time against the values in the database? If so, isn't this inefficient if it's always needing to pull values from a database based on time? What's the correct practice for something like this?

2 Answers2

0

You can setup a service and schedule that service to run periodically so that it does whatever job you want it to do

maybe this article can help you.

JMartins
  • 61
  • 1
  • 4
0

Alarm manager and services will be ideal for you to implement to do something for your requirement.

Services : It will be running all the time irrespective of your life-cycle of activity. Alarm manager: Inside services use alarm manager to trigger event to update UI at regular interval.

Finally you can use Local braodcast reciever to update your Activity from services.

You can check implemetation in details below :

Android update activity UI from service

Community
  • 1
  • 1
Justcurious
  • 2,201
  • 1
  • 21
  • 36
  • For AlarmManager I found http://stackoverflow.com/questions/24196890/android-schedule-task-to-execute-at-specific-time-daily but for some reason I don't seem to have "AlarmReceiver" – user7767733 Mar 25 '17 at 23:17