设置闹钟几天

时间:2016-11-19 15:57:40

标签: android alarmmanager

我正在研究闹钟应用。我遇到过一个问题。我好几天都不知道如何设置闹钟。我已经尝试了下面的代码但是在日志中我看到了这个Wed Apr 06(没有更改任何日期,所以应该最接近星期二和星期五)。我做错了什么?可能我应该每隔一天单独设置闹钟吗?

这是我的代码:

calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY, Calendar.FRIDAY);
                //calendar.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
                Log.e("Point_1","Calendar " + calendar.getTime());
                calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
                Intent intent1 = new Intent(MyService_alarm.this,MyReceiver_Alarm.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService_alarm.this,intent.getIntExtra("Size", 1),intent1,0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent);

谢谢。

2 个答案:

答案 0 :(得分:1)

这是因为您在设置时间选择器的小时和分钟之前记录时间,

你的代码工作正常但是要在日志中显示设置为警报的时间,你需要将Log.e移动到将日历设置为选择器的小时和分钟之后,所以你的代码应该看起来像这样:

calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY, Calendar.FRIDAY);
            //calendar.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
            calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
            Log.e("Point_1","Calendar " + calendar.getTime());
            Intent intent1 = new Intent(MyService_alarm.this,MyReceiver_Alarm.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService_alarm.this,intent.getIntExtra("Size", 1),intent1,0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent);

此外,由于您要发出警报数天,因此保存所有设置的警报并添加接收器以检测设备何时启动是明智的,因为您的警报在重新启动时被取消并且需要再次添加

答案 1 :(得分:0)

该行

calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY, Calendar.FRIDAY);

不正确。如果您查看documentation,您会看到没有这样的方法。

通过编写Calendar.DAY_OF_WEEK,您告诉系统您正在输入一些值作为星期几。但是,而不是一个特定的日子,你进入两个。 Calendar对象用于存储一个特定日期。

因此,为了将警报设置好几天,您需要分别设置每个警报。因此,您可以创建单独的Calendar个对象,或者通过更改时间来重用它们。但是,两个警报都有相同的接收器类。因此,您需要创建不同的待处理意图以使警报管理器区分它们。出于这个原因,我向您展示了具有不同request_code的示例。

calendar1.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
calendar2.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

calendar1.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar1.set(Calendar.MINUTE, timePicker.getCurrentMinute());

calendar2.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar2.set(Calendar.MINUTE, timePicker.getCurrentMinute());

Intent intent1 = new Intent(MyService_alarm.this, MyReceiver_Alarm.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MyService_alarm.this, 1, intent1, 0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MyService_alarm.this, 2, intent1, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, calendar1.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent1);
alarmManager.setRepeating(AlarmManager.RTC, calendar2.getTimeInMillis(), 7 * 24 * 3600 * 1000, pendingIntent2);