[英]Set Repeat days of week alarm in android
有人可以為設定的重復周日警報提供良好的邏輯嗎? 我通過使用每周做一次警報
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天后會自動觸發。
但我的要求是我想選擇幾天而不是僅僅七天。
類似於每周一,周二,周四上午9:00 - 警報應自動觸發。 我如何在setRepeating中執行此操作。
有人可以幫我解決這個問題嗎?
謝謝!
這些問題談論你想要的同樣的事情。 這些答案將有所幫助:
您只需指定開始日期,然后每7天重復一次。 在給定問題的答案中指定的方法很少:
更新:
在你的評論中你說
如何在setRepeating中設置triggerAtMillis部分。 比如說今天是星期二,我選擇每周一,周三,周五。 - 星期三我該怎么辦?
據我所知,如果今天是星期二,如何設置警報,讓我們說星期三重復,對吧? 首先是的,你可以使用mulltiple id來分別為每一天設置警報。
然后你可以添加alarmCalendar.set(Calendar.DAY_OF_WEEK, week);
與現有代碼對齊。 根據工作日(從1-7開始),它會在當天重復。 您可以將其作為參數傳遞給函數。 喜歡:
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);
}
我從上面的一個問題中得到了例子。 希望現在更清楚。
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);
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);
}
要設置工作日的重復警報,請使用以下代碼。 希望這是有幫助的。
Calendar calender= Calendar.getInstance();
calender.set(Calendar.DAY_OF_WEEK, weekNo); //here pass week number
calender.set(Calendar.HOUR_OF_DAY, hour); //pass hour which you have select
calender.set(Calendar.MINUTE, min); //pass min which you have select
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);
Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
if (calender.before(now)) { //this condition is used for future reminder that means your reminder not fire for past time
calender.add(Calendar.DATE, 7);
}
final int _id = (int) System.currentTimeMillis(); //this id is used to set multiple alarms
Intent intent = new Intent(activity, YourServiceClass.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, _id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.