在Android中使用AlarmManager启动日常服务?

Nat*_*ugh 3 android

我需要每晚午夜运行服务.我想使用AlarmManager来做到这一点.

你能给我一些如何让它正常工作的指导吗?

alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), AlarmManager.INTERNAL_DAY, serviceIntent);
Run Code Online (Sandbox Code Playgroud)

也许我需要使用Calendar对象来指定时间?谢谢你的帮助!

Com*_*are 6

setInexactRepeating()顾名思义,这是不准确的.请记住,它可能会在午夜之前或之后运行.

要确定午夜的时间,请使用Calendar对象.然后打电话getDate()来获取a DategetTime()on Date以获得适当的毫秒值setInexactRepeating().