这个时间事件监听器的解决方案非常棒: Time Event Listener
这是一个代码示例:
Calendar cal = Calendar.getInstance();
Intent activate = new Intent(context, Alarm.class);
AlarmManager alarms ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, activate, 0);
alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 00);
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
如何动态制作许多这些?我有一个int i
的for循环,包含用户想要设置闹钟的所有小时数。我想过创建Calendar(cal + i)的动态实例,但是我们没有用Java创建它。
答案 0 :(得分:1)
您可以更改日历值,然后使用cal.getTimeInMillis()
。你不需要几个Calendar实例。结果是原始的,而不是对象。
价:
http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()
答案 1 :(得分:0)
Juan answer是对的,您可以使用单个日历来计算许多时间戳,然后循环播放它们并安排闹钟。以下代码使用数组来跟踪时间戳并更具体地显示方法:
long[] times = new long[3];
Calendar cal = Calendar.getInstance();
times[0] = cal.getTimeInMillis(); // timestamp of now
cal.add(HOUR_OF_DAY, 1);
times[1] = cal.getTimeInMillis(); // in one hour
cal.add(MINUTES, 30);
times[2] = cal.getTimeInMillis(); // in one and a half hour
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, activate, 0);
for (long time : times) {
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
}
答案 2 :(得分:0)
谢谢大家,解决方案是重复代码并使用PendingIntent alarmIntent = PendingIntent.getBroadcast(this, i, activate, 0);
作为pendingIntent中的请求代码,如下所示:
function RGBtoHex (red, green, blue) {
red = Math.max(0, Math.min(~~this.red, 255));
green = Math.max(0, Math.min(~~this.green, 255));
blue = Math.max(0, Math.min(~~this.blue, 255));
return '#' + ('00000' + (red << 16 | green << 8 | blue).toString(16)).slice(-6);
};