Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time since first boot up

Tags:

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)

  1. The filename of each file I receive from server includes a timestamp taken from System.currentMillis()
  2. I compare those timestamps in order to determine, which file the most current one is.
  3. Now, the user changes system time a few months ahead.
  4. I am still able to determine the most current file downloaded after user changed system time.
  5. Now, the user changes time back to original setting.
  6. The file downloaded on step 4 always wins when comparing timestamps.

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.

like image 515
user2857033 Avatar asked Apr 25 '14 01:04

user2857033


2 Answers

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.

like image 115
ozbek Avatar answered Sep 21 '22 00:09

ozbek


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:

1. As a root/superuser

one would lookup the time-stamp of a directory/file that is known to get created on the Android device during first-boot.

2. As a regular app,

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; 

3. Alternately as a regular app,

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(); 
like image 36
TheCodeArtist Avatar answered Sep 24 '22 00:09

TheCodeArtist