调用方法每隔X分钟 [英] Call a method every x minutes
问题描述
目前我有一个可以发送通知的应用程序,我想这个应用程序能够自动发送这些通知每隔x分钟(例如15分钟是默认值)。该应用程序可能会或可能不会是当前正在运行时,此通知被发送。
currently I have a application which can send notifications, I would like this application to be able to send these notifications automatically every x minutes (for example, 15 minutes is the default). The app may or may not be currently running when this notification should be sent.
一些阅读后,我已经确定的AlarmManager将用在这里,但我无法弄清楚如何从它调用的方法。我已经通知code,我只是需要一个方法来调用它每隔x分钟。
After some reading, I've determined that an AlarmManager would be used here, but I cannot figure out how to call a method from it. I already have the notification code, I just need a way to call it every x minutes.
我很新的这一点,这是我第一次练习的应用程序(其在C#中的经验,但很少在Java中)。
I'm very new to this, this is my first practice app (having experience in C#, but little in Java).
谢谢你,大卫。
推荐答案
您可能要考虑这样的事情:
You might want to consider something like this:
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
// Start every 30 seconds
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
如果你不清楚,经过本教程
这篇关于调用方法每隔X分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!