我正在创建一个具有闹钟功能的时钟应用程序。时间显示正确,我也正确设置了多个警报。
我正在使用不同的 id 创建多个警报,并将其保存到数据库中,以便我可以在列表视图中查看警报列表。现在我正在尝试为我的闹钟设置 ON 和 OFF 功能。我那里有问题。
在 itemclick 上,如果警报打开,它会在以下帮助下关闭:
Intent intent = new Intent(Main.this,TaskRecieverForAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(Main.this, Integer.parseInt(cont[0]), intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi);
上面的代码很好地取消了警报。
要打开我正在使用的警报:
Intent intent = new Intent(Main.this, TaskRecieverForAlarm.class);
intent.putExtra("AlarmDate", cont[1]);
intent.putExtra("key", Integer.parseInt(cont[0]));
PendingIntent sender = PendingIntent.getBroadcast(Main.this, Integer.parseInt(cont[0]) , intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
if(type.equalsIgnoreCase("daily"))
{
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1440*60000 ,sender);
}
else if(type.equalsIgnoreCase("weekly"))
{
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 7*1440*60000 ,sender);
}
现在,只要我单击 OFF 到 ON,警报就会触发并调用 TASKReceiverFORAlarm(广播接收器),即使警报时间距当前时间还有 4 或 5 小时。我不确定我哪里出错了?
有人可以帮帮我吗?
谢谢!