在Android中使用AlarmManager推出了每日一班? [英] Using AlarmManager in Android to launch a daily service?
问题描述
我需要每天晚上在午夜运行的服务。我想用AlarmManager做到这一点。
I need to run a service each night at midnight. I would like to use the AlarmManager to do this.
您能给我如何使它正常工作,一些指导?
Can you give me some guidance of how to make it work correctly?
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), AlarmManager.INTERNAL_DAY, serviceIntent);
也许,我需要使用日历对象指定时间?感谢您的帮助!
Perhaps I need to use a Calendar object to specify the time? Thanks for any help!
推荐答案
setInexactRepeating()
,顾名思义,是不准确的。请记住,它可能之前或午夜之后有些运行。
setInexactRepeating()
, as the name suggests, is inexact. Bear in mind that it might run somewhat before or after midnight.
要确定何时午夜,使用日历
对象。然后调用 GETDATE()
获得日期
和的getTime()
的日期
,以获得正确的毫秒值的 setInexactRepeating()
电话。
To determine when midnight is, use a Calendar
object. Then call getDate()
to get a Date
and getTime()
on the Date
to get the proper millisecond value for your setInexactRepeating()
call.
这篇关于在Android中使用AlarmManager推出了每日一班?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!