【发布时间】:2011-01-28 21:34:08
【问题描述】:
我在使用 Android 的 Alarmmanager 功能时遇到问题。
问题是等待一个多小时左右的警报无法响起。
我的应用程序最初会像这样创建一个警报:-
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mCal.getTimeInMillis(), sender);
当闹钟响起时,它会触发我的 RecieverHandler 类,特别是这个函数:-
public void onReceive(Context context, Intent intent)
{
try {
Bundle bundle = intent.getExtras();
Intent newIntent = new Intent(context, MessageDispatcher.class);
newIntent.putExtras(bundle);
// newIntent.addFlags(Intent.FLAG);
context.startService(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
然后这会启动一个名为 MessageDispatcher 的服务,并调用此函数:-
public int onStartCommand(Intent intent, int flags, int startId)
这个函数从我的数据库中获取下一个闹钟时间,我确信它工作正常,然后它会根据数据库中的日期设置一个新的闹钟,如下所示:-
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mCal.getSendAt().getTimeInMillis(), sender);
这会为下一条消息创建警报。
我已经在很短的时间内对此进行了测试,它似乎可以正常工作,并且通过更改我在手机中的日期和时间已经在很长一段时间内对其进行了测试。它似乎成功启动。
然后,当此警报响起时,它会获取下一个警报响起并安排此时间。我几乎 100% 确定这些部件工作正常。
所以我只坚持一些关于它为什么不起作用的理论。
我认为这可能与我将手机与调试器断开连接有关,但在这种情况下,警报似乎在很短的时间内起作用。
所以我的主要理论是我正在创建的警报管理器在一定时间后被删除?如果这是真的,这是一个大问题,因为无论过去多久我都需要它工作。
非常感谢任何有助于确保我的闹钟仍然存在的帮助,谢谢。
【问题讨论】:
标签: android alarmmanager