Reputation: 73
I do a function for setRepeating every Monday, every 1st of month and 1st of january , I do this for every Monday:
GregorianCalendar date = new GregorianCalendar();
while( date.get( Calendar.DAY_OF_WEEK ) != Calendar.MONDAY )
date.add( Calendar.DATE, 1 );
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingintentResetAlarms);
My problem is that I don't know how I can do for every 1st of every month and 1st of January because every month has different numbers of days. I need ideas.
Thanks
Upvotes: 0
Views: 371
Reputation: 73
the receiver:
final BasedeDatos bd = new BasedeDatos(context, "DBSesiones", null, 1);//parametros:contexto,nombre base de datos,
SQLiteDatabase db = bd.getWritableDatabase();
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
boolean bRestAlarms = bundle.getBoolean("ReseteaBBDD", false);
if (bRestAlarms) {
//call your subbroutine here
GregorianCalendar date = new GregorianCalendar();
if(date.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY){
//reseteo tabla semana
for(int i=0;i<6;i++) {
db.execSQL("UPDATE " + "SesionesSemana" + " SET " + columnas[i] + " = " + 0 + " WHERE " + "_id = 1");
}
} if(date.get(Calendar.DAY_OF_MONTH) == 1){//si es primero de mes
//reseteo la tabla meses
for(int i=0;i<6;i++) {
db.execSQL("UPDATE " + "SesionesMes" + " SET " + columnas[i] + " = " + 0 + " WHERE " + "_id = 1");
}
}
if(date.get(Calendar.DAY_OF_MONTH)==1 && date.get(Calendar.MONTH)==Calendar.JANUARY){
//reseteo la tabla años
for(int i=0;i<6;i++) {
db.execSQL("UPDATE " + "SesionesAno" + " SET " + columnas[i] + " = " + 0 + " WHERE " + "_id = 1");
}
}
db.close();
}
}
}`
Upvotes: 0
Reputation: 6136
I think you can do this by something like following and set the alarm every 365 days:
GregorianCalendar date = new GregorianCalendar();
date.set(Calendar.MONTH, Calendar.JANUARY);
date.set(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), 365 * 24 * 60 * 60 * 1000, pendingintentResetAlarms);
date.set(Calendar.MONTH, Calendar.FEBRUARY);
date.set(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), 365 * 24 * 60 * 60 * 1000, pendingintentResetAlarms);
...
However there is a question about having 365 days or 366 days a year. Well another solution is maybe to avoid that and do something like:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingintentResetAlarms);
it triggers the alarm every day and in your broadcast receiver, you can check the current day:
GregorianCalendar date = new GregorianCalendar();
if(date.get(Calendar.DAY_OF_MONTH) == 1)
....
Actually I think the second solution is better and more robust
Upvotes: 0