2013-07-27 22 views
10

週アラームの設定された繰り返し日のために誰かが良いロジックを与えることができますか?私は毎週行っているアラームを使用してアンドロイドで週アラームの繰り返しの日を設定する

  alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt); 
      alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt); 
      alarmCalendar.set(Calendar.SECOND, 0); 
      alarmCalendar.set(Calendar.AM_PM, amorpm); 

      Long alarmTime = alarmCalendar.getTimeInMillis(); 

Intent intent = new Intent(Alarm.this, AlarmReciever.class); 
       intent.putExtra("keyValue", key); 
       PendingIntent pi = PendingIntent.getBroadcast(Alarm.this, key, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
       am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7*1440*60000 , pi); 

アラームトリガーは、時間と7日後に自動的にその時にトリガーします。

しかし私の要件は、ちょうど7日ではなく日を選択したいということです。

毎週月曜日、火曜日、木曜日午前9:00のようなもの - アラームは自動的にトリガーされるはずです。 setRepeatingでこれをどうやって行うのですか?

誰かがこれで私を助けることができますか?

ありがとうございます!

答えて

12

これらの質問は、あなたが望むのと同じことを話します。これらの回答は参考になります:

開始するには曜日を指定し、7日ごとにそれを繰り返すだけです。与えられた質問に答えで指定されたいくつかの方法があります。

How can i get the repeat alarm for week days using alarm manager?

Android Notification on specific weekday goes off directly

how to repeat alarm week day on in android

更新:あなたのコメントで

は、あなたが言った

setRepeatingでtriggerAtMillisパーツを設定する方法。たとえば今日は火曜日、私は毎週月曜日、水曜日、金曜日を選択します。 - 水曜日に何を置くの?

私は、今日が火曜日なら、アラームを設定する方法は水曜日が繰り返されると言うことができますか?まず第一に、あなたは毎日のアラームを別々に設定するために複数のidを使うことができます。

次に、既存のコードにalarmCalendar.set(Calendar.DAY_OF_WEEK, week);行を追加できます。 1日(1〜7日)に基づいて、その日に繰り返されます。これをパラメータとして関数に渡すことができます。 Like:

setAlarm(2); //set the alarm for this day of the week 

    public void setAlarm(int dayOfWeek) { 
     // Add this day of the week line to your existing code 
     alarmCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek); 

     alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt); 
     alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt); 
     alarmCalendar.set(Calendar.SECOND, 0); 
     alarmCalendar.set(Calendar.AM_PM, amorpm); 

     Long alarmTime = alarmCalendar.getTimeInMillis(); 
     //Also change the time to 24 hours. 
     am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 , pi); 
} 

私は上記の質問の1つから例を取り上げました。今、より明確になることを願っています。

+0

あなたは、私は別の警報の日に別のIDを設定する必要が言うことを意味します。しかし、setRepeatingでtriggerAtMillisの部分を設定する方法。 たとえば今日は火曜日ですが、私は毎週月曜日、水曜日、金曜日を選択します。 - 水曜日のトリガーは、現在の時刻から1日何を置くのですか? – TheDevMan

+0

アラームごとに異なるIDです。あなたの第2部については、更新された答えを見てください。 –

+0

@Shobit Puri週日以上にアラームを設定したいのですが、毎週水曜日、水曜日のように毎週設定する方法があります。トリガ時間を7日に設定した場合、次の日が火曜日の場合、それは動作しません。手伝ってくれますか? – moDev

1
Intent intent1 = new Intent(getApplicationContext(), 
          NotificationReceiver.class); 
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 
                  1, 
                  intent1, 
                  PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE); 
java.util.Calendar calendar1 = java.util.Calendar.getInstance(); 

calendar1.set(java.util.Calendar.DAY_OF_WEEK, 
       Calendar.MONDAY); 
calendar1.set(java.util.Calendar.HOUR_OF_DAY, 
       22); 
calendar1.set(java.util.Calendar.MINUTE, 
       8); 
calendar1.set(java.util.Calendar.SECOND, 
       0); 

alarmManager1.setExact(AlarmManager.RTC, calendar1.getTimeInMillis(), pendingIntent1); 
0
if (Monday.isChecked()) { 
         setalarm(2); 
        } else if (Tuesday.isChecked()) { 
         setalarm(3); 
        } else if (Wednesday.isChecked()) { 
         setalarm(4); 
        } else if (Thursday.isChecked()) { 
         setalarm(5); 
        } else if (Friday.isChecked()) { 
         setalarm(6); 
        } else if (Saturday.isChecked()) { 
         setalarm(7); 
        } else if (Sunday.isChecked()) { 
         setalarm(1); 
        } 

public void setalarm(int weekno) { 

     cal.set(Calendar.DAY_OF_WEEK, weekno); 
     cal.set(Calendar.MINUTE, min); 
     cal.set(Calendar.SECOND, 0); 
     cal.set(Calendar.MILLISECOND, 0); 

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 


    cal.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent); 
} 
関連する問題