Search code examples
androidservicetimertimer-jobs

Service should load data every hour


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


Solution

  • 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.