I’m trying to make an application that allows the user to access some information only once a day else to show the user how longer does he have to wait until it can see again that information.
Here is my code:
long aux_time = prefs.getLong("AUX",System.currentTimeMillis());
Log.v(TAG, "aux=" + aux_time);
Log.v(TAG, "time" + System.currentTimeMillis());
if(System.currentTimeMillis() > aux_time + (10000 * 60 * 60 * 24))
{
finish();
Intent intent = new Intent(Castle.this, Hug.class);
startActivity(intent);
}
if (System.currentTimeMillis() >= aux_time + (10000 * 60 * 60 * 24))
{
SharedPreferences.Editor editor = prefs.edit();
editor.putLong("AUX", System.currentTimeMillis());
editor.commit();
finish();
Intent intent = new Intent(Castle.this, Hug_Accepted.class);
startActivity(intent);
}
I tested the code. And, I believe, it does open after 24h, but it doesn’t opens when I first open the Activity because there is a difference of 0.00000000001 ms between those two when I compare them in the second if. Any ideas?
In addition to Jave’s answer
if.(System.currentTimeMillis() > aux_time + (1000 * 60 * 60 * 24))
Here you check whether the current time is greater than aux_time by
at least 24 hours.