【发布时间】:2016-04-14 17:33:58
【问题描述】:
我需要在 Android 中做一个函数或类,我可以在某些日期重置表 SQLITE 的某些值。
例如,星期一 0:00 点设置零值,每月 1 日设置零另一个值,每年 1 月 1 日将其他值设置为零。
如果应用程序的用户不必触摸任何东西,我怎么能自动执行此操作?
【问题讨论】:
标签: android sqlite date datetime reset
我需要在 Android 中做一个函数或类,我可以在某些日期重置表 SQLITE 的某些值。
例如,星期一 0:00 点设置零值,每月 1 日设置零另一个值,每年 1 月 1 日将其他值设置为零。
如果应用程序的用户不必触摸任何东西,我怎么能自动执行此操作?
【问题讨论】:
标签: android sqlite date datetime reset
你可以使用闹钟:
@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);
你可以对月初和年初有同样的想法。
【讨论】: