I'm developing an android application and hit the problem with determining system first boot up time. I mean i need to measure how much time already passed from device first boot up.
I know about solution with listening for ACTION_BOOT_COMPLETED and save anything in SharedPreferences, but i need another solution, because this one does not work for some cases. Maybe there is any system property?
Use case (excerpt from discussion)
System.currentMillis()
The silver bullet to solve this problem would be a timestamp that counts seconds since first boot (after factory reset). Just like SystemClock.elapsedRealtime()
but without reset after each boot. Unfortunately, the answers so far tell us, that this silver bullet doesn't exist.
However, many answers show a great variety of options how to tackle that problem. OneWorld123 commented each answer, how that suited his needs.
Maybe there is any system property?
Not sure about system property, but there is SystemClock
class which provides API's to get system uptime:
SystemClock.uptimeMillis()
which
Returns milliseconds since boot, not counting time spent in deep sleep.
You may also use SystemClock.elapsedRealtime()
which
Returns milliseconds since boot, including time spent in sleep.
Hope this helps.
In case one needs to know when was the first time an Android device was booted,
The easiest method would be to have an application
- that is installed in the factory image
- that is configured to run during boot
- that logs the current date & time into a
sharedPreference
on its first run
Subsequently any other apps that need to determine the first boot time of the Android device can lookup the appropriate sharedPreference
during the lifetime of the device. (or until the device is factory-reset; at which point the pre-installed app would write the new date&time into the shared preference after a reboot.)
However if it is not possible to an pre-install an application on the Android device, then a couple of potential workarounds would be:
one would lookup the time-stamp of a directory/file that is known to get created on the Android device during first-boot.
a simple workaround method using standard Android APIs would be to check for the installation-time of an appropriate system package that is known to get installed during first-boot.
/* This returns the last time a package was installed */ PackageManager pm = context.getPackageManager(); PackageInfo pInfo = pm.getPackageInfo(<app-package-name>, 0); return pInfo.firstInstallTime;
if we can rely on a specific package being updated one-time during first-boot (and never again) we can check its update-time as follows:
/* This returns the last time a package was updated */ PackageManager pm = context.getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo(<app-package-name>, 0); String appFile = appInfo.sourceDir; long installed = new File(appFile).lastModified();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With