So you are programming an application for Android. You have made a custom caching system which should clear the cache after each boot. One way to do this, could be to create an onBoot Receiver and have that clear out your cache. However, why have your application run on each boot if the user might not even use it all that much? And what if the user installed some Privacy Guard Application and disabled your app's receiver?
The best way to handle this, is to have the cache cleared on the first launch of the application. But in order to do this, you will have to know whether or not this actually is the first launch or not. To help you with this, Android has two useful methods. One to provide the total amount of milliseconds that have past since 1970 (or something like that) and one to provide the total amount of milliseconds that have pasted since the device was booted. Extract the boot time from the total time, and you will have the exact timestamp from when the device was started. Now just save this to your shared preferences and compare it with a fresh timestamp on each application launch.
public class myclass extends something {
/*
* We can use this to avoid to much checking.
* As long as this static property exists, we know that device has not been rebooted and there is no reason to do a check.
*/
private static Boolean oCacheCheck = false;
public SharedPreferences getCache() {
SharedPreferences preferences = .getSharedPreferences("cache", 0x00000000);
if (!oCacheCheck) {
Long freshTime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
Long cachedTime = preferences.getLong("timestamp", 0);
if (freshTime == cachedTime) {
Editor edit = preferences.edit();
edit.clear();
edit.putLong("timestamp", freshTime);
edit.commit();
}
oCacheCheck = true;
}
return preferences;
}
}
if (freshTime == cachedTime) {
There is however one tiny issue with this way of checking last boot time, and that is that both methods might take a few milliseconds to execute (depending on the speed of the device) and you can only execute one at a time. This means that your calculation could differ each time you run it, making the comparison with the cached timestamp useless.
The solution to this problem is to check the difference between the two timestamps instead of comparing them to see if they are equal. The methods might take a few milliseconds to execute and any type of device will take several seconds, some even minutes, to boot. So we will just check to see if the two timestamps has a difference less than 3 seconds.
public class myclass extends something {
/*
* We can use this to avoid to much checking.
* As long as this static property exists, we know that device has not been rebooted and there is no reason to do a check.
*/
private static Boolean oCacheCheck = false;
public SharedPreferences getCache() {
SharedPreferences preferences = .getSharedPreferences("cache", 0x00000000);
if (!oCacheCheck) {
Long freshTime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
Long cachedTime = preferences.getLong("timestamp", 0);
/*
* If this is grater than 3 second, then we will most likely have a fresh application launch.
* No device, no mater how slow, takes 3 seconds or more to execute the time methods.
* And no device, no mater how fast, can boot and launch the application in less than 3 seconds.
*/
if ((freshTime - cachedTime) > 3000) {
Editor edit = preferences.edit();
edit.clear();
edit.putLong("timestamp", freshTime);
edit.commit();
}
oCacheCheck = true;
}
return preferences;
}
}
if ((freshTime - cachedTime) > 3000) {
If, when you extract the cached timestamp from the fresh one, get a difference on more than +3000 milliseconds, you can be sure that the device has been rebooted since your last application launch.