在Android中使用AlarmManager推出了每日一班?推出了、Android、AlarmManager

2023-09-05 09:38:16 作者:星河与你.

我需要每天晚上在午夜运行的服务。我想用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.

 
精彩推荐