Question

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

Was it helpful?

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top