0

特定の日を選択してアラームを作成しています。ある時点でそれらの曜日にアラームを繰り返したい。しかし、アラームを作成するとすぐに、過去および将来の曜日が選択されているときにアラームが発生します。

たとえば、今日を「水曜日」と考えて、火曜日と木曜日を選択すると、作成時にアラームが発生します。ただし、次の火曜日と明日にそれぞれ起動する必要があります。次のコードで間違いを見つけることができません。

私がやっている間違いを見つけることができません。

//scheduling the alarm for repeating on selected days
public void scheduleDay(int request_code_value, String alarm_title) 
{

   Calendar calendar = Calendar.getInstance();


   for(int i = 1; days.size() >= i; i++)
   {
       if ((days.get("" + i)) == 1)
       {
           // for creating different alarms adding i to request_code_value  for uniqueness
           request_code_value = request_code_value +i;

           String filter_action = "com.ibkr.yowakeup" + request_code_value +"_time";


            IntentFilter filter = new IntentFilter(filter_action);

            registerReceiver(new AlarmReciever(), filter);


            Intent intent = new Intent(filter_action);
            intent.putExtra(getString(R.string.get_current_intent_value), request_code_value);
            intent.putExtra(getString(R.string.alarmtext), alarm_title);
            intent.putExtra(getString(R.string.alarm_time), newAlarm_Choose_Alarm_Value.getText().toString());


            Log.d(TAG,"Scheduled on " + i + " = " + days.get("" + i));

            calendar.set(Calendar.DAY_OF_WEEK, i);
            calendar.set(Calendar.HOUR_OF_DAY, selected_hour);// cal.get(Calendar.HOUR_OF_DAY);
            calendar.set(Calendar.MINUTE, selected_min);// cal.get(Calendar.MINUTE);
            calendar.set(Calendar.SECOND, 0);// cal.get(Calendar.SECOND);
            calendar.set(Calendar.MILLISECOND, 0);


            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, request_code_value , intent, 0);

            alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 
                    AlarmManager.INTERVAL_DAY * 7,
                    pendingIntent);

        }
   }

}

私を助けてください。前もって感謝します。

4

1 に答える 1

0

を操作してカレンダーを設定しようとしていDAY_OF_WEEKます。インスタンスは常に特定のCalendar時点を表します。呼び出すと、現在の日付と時刻に初期化されCalendar.getInstance()たインスタンスが提供されます。Calendar例を使用しましょう:

私は今日、2015 年 3 月 8 日日曜日の 08:00 にこのコードを実行しています。CalendarI get from Callingは、Calendar.getInstance()この日付/時刻に初期化されます。今日は日曜日なので、DAY_OF_WEEK呼び出した場合に得られるの値calendar.get(Calender.DAY_OF_WEEK)Calendar.SUNDAY1 です。

火曜日にアラームを設定したいとしましょう。この場合、コードは次を呼び出します。

calendar.set(Calendar.DAY_OF_WEEK, i);

whereは、値が 3 にi設定されています。Calendar.TUESDAY

今何が起こるべきですか?インスタンスには、Calendar実際には日付/時刻のこれらすべての部分に個別のフィールドがありません。特定の日付/時刻を表す単一の値のみを保持します。そうしないと、 を に設定し、さらにを 9 (3 月は月曜日)に設定しようとすると、矛盾が生じる可能性がありますDAY_OF_WEEK。また、日付を前の に調整するか、次の に調整する必要があるかを に伝える方法はありません。おそらくこれを決定する方法はあるでしょうが、私はそれをしません。SUNDAYDAY_OF_MONTHCalendarSUNDAYSUNDAY

このため、のようなフィールドを設定 しようとしないでください。代わりに、 を呼び出して現在の曜日を特定し、目的の曜日になるように正しい日数を追加してカレンダー インスタンスを前方に調整する必要があります。このような:CalendarDAY_OF_WEEKcalendar.get(Calendar.DAY_OF_WEEK)

int desiredDay = // the day of the week you want to set the alarm for
int today = calendar.get(Calendar.DAY_OF_WEEK);
int numberOfDaysToAdd = desiredDay - today;
if (desiredDay < today) {
    // Desired day is earlier in the week than today, add 7 days to
    //  ensure it is in the future
    numberOfDaysToAdd += 7;
}
calendar.add(Calendar.DAY, numberOfDaysToAdd);

アラームの目的の時刻がすでに過ぎているかどうかを確認し、同様のアルゴリズムを使用して将来の日付に設定する必要がある場合があります。うまくいけば、あなたはアイデアを得る。

于 2015-03-08T17:30:06.367 に答える