在Android中使用AlarmManager推出了每日一班? [英] Using AlarmManager in Android to launch a daily service?

查看:79
本文介绍了在Android中使用AlarmManager推出了每日一班?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆