0

它是关于一个小型 android 应用程序,它将在一周中的每一天花费数小时作为用户的输入,并在相应的日子相应地打开/关闭蓝牙设备,而不管“日期”如何。即代码应该只检查日期、小时和分钟。我使用了以下代码(用于今天即星期五的测试目的),但它不会触发警报。

//..........setting calender for MyAlarmService
       Calendar cur_cal = new GregorianCalendar();
       cur_cal.setTimeInMillis(System.currentTimeMillis());
       Calendar cal = new GregorianCalendar();
       cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
       cal.set(Calendar.HOUR_OF_DAY, 11);
       cal.set(Calendar.MINUTE, 5);
       cal.set(Calendar.SECOND, 0);
       cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
       cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
       cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
       // here it m setting the "Today"
       cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); 

       Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
       pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

当我删除语句 cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

AlarmService 在上面指定的时间(即上午 11:05)完美触发“今天”

我究竟做错了什么?

4

1 回答 1

0

找到解决方法如下。

 //..........setting calender for Friday


       Calendar calFri = new GregorianCalendar();
       calFri.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
       calFri.set(Calendar.HOUR_OF_DAY, friHrInt);
       calFri.set(Calendar.MINUTE,friMinInt);
       calFri.set(Calendar.SECOND, 0);
       calFri.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
       calFri.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
       calFri.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

       days = 13 - calFri.get(Calendar.DAY_OF_WEEK); // how many days until Sunday
       calFri.add(Calendar.DATE, days);

     //..........setting calender for Saturday


       Calendar calSat = new GregorianCalendar();
       calSat.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
       calSat.set(Calendar.HOUR_OF_DAY, satHrInt);
       calSat.set(Calendar.MINUTE,satMinInt);
       calSat.set(Calendar.SECOND, 0);
       calSat.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
       calSat.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
       calSat.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

       days = 14 - calSat.get(Calendar.DAY_OF_WEEK); // how many days until Sunday
       calSat.add(Calendar.DATE, days);

然后我这些天使用分离警报服务如下

 //.........................The Following will make the alarm go off on friday......................................


           Intent myIntentFri = new Intent(AndroidAlarmService.this, FriOffAlarmService.class);
           pendingIntentFri = PendingIntent.getService(AndroidAlarmService.this, 0, myIntentFri, 0);
           AlarmManager alarmManagerFri = (AlarmManager)getSystemService(ALARM_SERVICE);
           //alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent2);
           alarmManagerFri.setRepeating(AlarmManager.RTC_WAKEUP, calFri.getTimeInMillis(),interval, pendingIntentSun);

           //.........................The Following will make the alarm go off on saturday......................................


           Intent myIntentSat = new Intent(AndroidAlarmService.this, SatOffAlarmService.class);
           pendingIntentSat = PendingIntent.getService(AndroidAlarmService.this, 0, myIntentSat, 0);
           AlarmManager alarmManagerSat = (AlarmManager)getSystemService(ALARM_SERVICE);
           //alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent2);
           alarmManagerSat.setRepeating(AlarmManager.RTC_WAKEUP, calSat.getTimeInMillis(),interval, pendingIntentSat);
于 2012-05-07T05:01:48.307 回答