Service should load data every hour

1.3k Views Asked by At

Question


I'm creating a service for my app, but there's probably pritty simple thing I can't realize. Better I say I don't know how to do this probably.

I'd like that my Service refreshs the data every hour if its between 6am and 6pm. How is this possible in a easy way which is also battery-efficient. If you have a some code snippets or a great tutorial for me I would be very happy, because I really like to learn this.

Best Regards

safari

3

There are 3 best solutions below

7
On BEST ANSWER

You can use AlarmManager to start the proceedings. First of all you need to check the current time that is current hour which you can get using Calender.HOUR_OF_DAY. Then you can check the current hour is between 6 to 18. If the condition satisfies you can refresh the data else you can cancel the Alarm and set it to the next period again.

0
On

Create an alarm which when fired refreshes your data and sets a new alarm one our (or 13 hours) in the future depending on current time (past 6pm). See here for more info on the alram manager: http://developer.android.com/reference/android/app/AlarmManager.html

1
On

you need a broadcast receiver that will fire the intent at 6 am to stop the service and at 6 pm to again start the service. By this way you can also improve battery consumtion.