1

我试图在用户指定的时间调用一个方法。例如,“下午 4:00 - 向 Bob 发送消息”。

我了解如何获取calendar.HOUR(),calendar.MINUTE()等;但是,我不确定在哪里放置此代码。我想确保该方法在正确的时间调用,但我也不想每秒更新calendar time一次(因为担心 RAM 的电池使用量过多)。

我目前正在使用这个,它在onCreate方法中。

    if (calendarHour == startHour && calendarMinute == startMinute)
{
//do stuff
}

是否有关于如何在不发送垃圾邮件的情况下检查现在几点的标准?我将使用什么代码以及将其放置在哪里?

4

1 回答 1

3

使用警报管理器

Calendar cal; // set your desired time
PendingIntent operation = PendingIntent.getService( /*  register a service that "sends a message to Bob"*/ );

final AlarmManager am = context.getSystemService(Context.ALARM_SERVICE).
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), operation);
// you can close your app now.
于 2013-04-20T20:04:26.680 回答