0
我按照this thread中的说明创建了每天12:30开始服务的每日闹铃,并显示通知。我遇到的问题是,每次应用程序启动时,设置闹钟也会触发服务(=>和通知)。防止Android日常闹铃在闹铃设置期间运行该意图
因为我认为闹钟只能在指定的时间(12:30)运行,所以在设置应用程序时没有问题。
我意识到,每次启动应用程序时从头开始设置闹钟有点无效,因为它只需要设置一次(我也将其设置为启动设备),但它似乎是最简单的方法。
那么解决这个问题的最好方法是什么?有没有办法在指定时间设置闹钟,而无需在设置时运行?
下面的代码,如果你有兴趣(启动应用程序时,这个函数被调用每次):
public static void setAlarm(Context context)
{
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction("com.Rani.app.SET_NOTIFICATION_ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar dailyCheckTime = Calendar.getInstance();
dailyCheckTime.setTimeZone(TimeZone.getTimeZone("GMT"));
dailyCheckTime.set(Calendar.HOUR_OF_DAY, 12);
dailyCheckTime.set(Calendar.MINUTE, 30);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, dailyCheckTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
在此先感谢。
随机猜测,你有没有尝试过的日期设置为明天? 'dailyCheckTime.set(Calendar.DAY_OF_MONTH,明天);'明天将是一个计算变量。伪:calendar.getDayOfMonth()+ 1 – Blundell 2012-03-11 22:58:22
我试过了,但它仍然运行它。我在下面发布了解决方案 – Rani 2012-03-12 23:52:43