我在使用 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% 确定这些部件工作正常。
所以我只坚持一些关于为什么它不起作用的理论。
我认为这可能与我将手机与调试器断开连接有关,但在这种情况下,警报似乎在短时间内起作用。
所以我的主要理论是我正在创建的警报管理器在一定时间后被删除?如果这是真的,这是一个大问题,因为无论过去了多少时间,我都需要它工作。
非常感谢任何有助于确保我的警报仍然存在的帮助,谢谢。