【问题标题】:How could I change values in database sqlite time to time automatically in android?如何在android中不时自动更改数据库sqlite中的值?
【发布时间】:2016-04-14 17:33:58
【问题描述】:

我需要在 Android 中做一个函数或类,我可以在某些日期重置表 SQLITE 的某些值。
例如,星期一 0:00 点设置零值,每月 1 日设置零另一个值,每年 1 月 1 日将其他值设置为零
如果应用程序的用户不必触摸任何东西,我怎么能自动执行此操作?

【问题讨论】:

    标签: android sqlite date datetime reset


    【解决方案1】:

    你可以使用闹钟:

        @Override 
        public void onCreate(Bundle icicle) {
           super.onCreate(icicle);
           Intent intent = new Intent("com.mycompany.myapp");
           intent.putExtra("DoTheWork", true);
           DateTime today = new DateTime().withTimeAtStartOfDay();
           DateTime tomorrow = today.plusDays(1).withTimeAtStartOfDay();
           pendingintentResetAlarms = PendingIntent.getBroadcast(con, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
           alarmManager.cancel(pendingintentResetAlarms);
           alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, tomorrow.getMillis(), 86400000, pendingintentResetAlarms);
           AlarmReceiver receiver = new AlarmReceiver();
           this.registerReceiver(receiver, new IntentFilter(com.mycompany.myapp));.
        }
    

    有一个接收器:

        private class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                boolean bRestAlarms = bundle.getBoolean("DoTheWork", false);
                if (bRestAlarms) {
                    //call your subbroutine here
                }
            }
    
        }
    }
    

    不要忘记在你的 onDestroy 中取消注册接收器。

    编辑 使用 Calendar 对象查找每个星期一:

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

    你可以对月初和年初有同样的想法。

    【讨论】:

    • 谢谢,但我如何配置每个星期一、每个月的第一天和每年的第一天的闹钟?还有DateTime类,在哪里??
    • @elvega 更新了答案。我回答了你的第一个问题,但你必须做一些谷歌搜索或弄清楚其他两个问题的逻辑。
    • pendingintentResetAlarms和con是什么类型的?
    • pendingintentResetAlarms、con和alarmManager是什么类型的?未声明
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    相关资源
    最近更新 更多