我想做这样的事情:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
int i = (int) (new Date().getTime()/1000);
if( ) // next day
{
mymethod();
}
}
当系统日期是新的一天时,我想调用mymethod()
最佳答案
新的一天是指午夜之后吗?那么您想检测日期是否与上次不同?
@Override
protected void onResume() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
int lastTimeStarted = settings.getInt("last_time_started", -1);
Calendar calendar = Calendar.getInstance();
int today = calendar.get(Calendar.DAY_OF_YEAR);
if (today != lastTimeStarted) {
//startSomethingOnce();
SharedPreferences.Editor editor = settings.edit();
editor.putInt("last_time_started", today);
editor.commit();
}
}
关于java - 如果日期是第二天,则运行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958758/