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 時 5 分に「今日」完全にトリガーされます。

私は何を間違っていますか?

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 に答える