当前位置: 动力学知识库 > 问答 > 编程问答 >

android - 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

网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: