感謝Techfist ..例如:DATEVALUE = 「30/01/2017 11:02」 ......從數據庫中獲取ID和日期等
private void getMonthlyDuration(Context context,int id,String dateValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences("ModernDiary", Context.MODE_PRIVATE);
String dateOfMonth = sharedPreferences.getString("day"+id,DateFormat.format("dd", Calendar.getInstance()).toString());
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
try {
Date dateMain = simpleDateFormat.parse(dateValue);
calendar.setTime(dateMain);
} catch (ParseException e) {
e.printStackTrace();
}
Boolean isSetDate = false;
if (sharedPreferences.getInt("monthInc"+id,-1) != -1) {
calendar.set(Calendar.MONTH,sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH)));
calendar.set(Calendar.YEAR,sharedPreferences.getInt("yearInc"+id,calendar.get(Calendar.YEAR)));
if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) {
calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+" max");
} else {
calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth));
Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+"min");
}
if (sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH)) < calendar.get(Calendar.MONTH)){
calendar.add(Calendar.MONTH, -1);
isSetDate = true;
Log.i("timeMonth","Increment "+calendar.getTime());
} else {
isSetDate = false;
Log.i("timeMonth","No Change");
}
}
calendar.add(Calendar.MONTH, 1);
if (isSetDate){
if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) {
calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
} else {
calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth));
}
}
Log.i("timeAf",calendar.getTime()+"");
sharedPreferences.edit().putInt("monthInc"+id, calendar.get(Calendar.MONTH)).apply();
sharedPreferences.edit().putInt("yearInc"+id, calendar.get(Calendar.YEAR)).apply();
Intent notificationIntent = new Intent(context,AlarmBroadcastReceiver.class);
Bundle bundle = new Bundle();
bundle.putSerializable("alarm", id);
notificationIntent.putExtra("bundle", bundle);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
broadcast = PendingIntent.getBroadcast(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
}
報警觸發時間: Tue Feb 28 11:02:00 GMT+05:30 2017 Thu Mar 30 11:02:00 GMT+05:30 2017 Sun Apr 30 11:02:00 GMT+05:30 2017 Tue May 30 11:02:00 GMT+05:30 2017 Fri Jun 30 11:02:00 GMT+05:30 2017 Sun Jul 30 11:02:00 GMT+05:30 2017 Wed Aug 30 11:02:00 GMT+05:30 2017 Sat Sep 30 11:02:00 GMT+05:30 2017 Mon Oct 30 11:02:00 GMT+05:30 2017 Thu Nov 30 11:02:00 GMT+05:30 2017 Sat Dec 30 11:02:00 GMT+05:30 2017 Tue Jan 30 11:02:00 GMT+05:30 2018 Wed Feb 28 11:02:00 GMT+05:30 2018 ...
檢查該鏈接,每月重複報警[這裏] [1] [1]:http://stackoverflow.com/questions/26645749/android-repeating-alarm-should-repeat-on-monthly-basis-and-on-same-day-for-each/ 26646069#26646069 – mushahid 2014-10-30 06:08:32