alarmManager setRepeating每周一,每月1日和1月1日安卓

时间:2016-04-16 13:59:32

标签: android date alarmmanager

我在每个星期一,每月1日和1月1日为setRepeating做一个函数,我每个星期一都这样做:

public class UserRoleCondition<T> : TypedQueryableStringOperatorCondition<T, IndexedContact> where T : VisitorRuleContext<IndexedContact>
{
    protected override Expression<Func<IndexedContact, bool>> GetResultPredicate(T ruleContext)
    {
        var userrole = this.Value ?? string.Empty;

        return
            this.GetCompareExpression(
                c => (string)c[(ObjectIndexerKey)"contact.profileproperties.userrole"], userrole);

    }
}

我的问题是我不知道每个月的每个月1日和1月1日我怎么办,因为每个月都有不同的天数。 我需要点子。

由于

2 个答案:

答案 0 :(得分:0)

我认为您可以通过以下内容完成此操作并每隔365天设置一次警报:

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);
...

然而,有一个问题是一年有365天或366天。另一种解决方案可能是避免这种情况并执行以下操作:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingintentResetAlarms);

它每天触发警报,在您的广播接收器中,您可以查看当天:

GregorianCalendar date = new GregorianCalendar();
if(date.get(Calendar.DAY_OF_MONTH) == 1)
   ....

实际上我认为第二种解决方案更好,更强大

答案 1 :(得分:0)

接收者:

 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();
        }
    }

}`