目前我有一个可以发送通知的应用程序,我希望该应用程序能够每 x 分钟自动发送一次这些通知(例如,默认为 15 分钟)。当应发送此通知时,应用程序当前可能正在运行,也可能没有运行。
经过一番阅读,我确定此处将使用 AlarmManager,但我不知道如何从中调用方法。我已经有了通知代码,我只需要一种每 x 分钟调用它的方法。
我对此很陌生,这是我的第一个练习应用程序(有 C# 经验,但 Java 经验很少)。
谢谢大卫。
最佳答案
您可能需要考虑这样的事情:
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);
如果不清楚,请查看this教程
关于java - 每 x 分钟调用一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16485988/