Skip to content

Instantly share code, notes, and snippets.

@logickoder
Created December 10, 2024 06:01
Show Gist options
  • Save logickoder/a7fa0674f04bb582d0198d07259a794a to your computer and use it in GitHub Desktop.
Save logickoder/a7fa0674f04bb582d0198d07259a794a to your computer and use it in GitHub Desktop.
Check if an android device restarted without depending on BOOT_COMPLETED
object DeviceRestartChecker {
private const val PREFS_NAME = "device_restart_prefs"
private const val KEY_LAST_UPTIME = "last_uptime"
operator fun invoke(context: Context): Boolean {
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val savedUptime = prefs.getLong(KEY_LAST_UPTIME, 0L)
val currentUptime = SystemClock.uptimeMillis()
return if (currentUptime < savedUptime) {
// Device has restarted (uptime reset)
true
} else {
prefs.edit()
.putLong(KEY_LAST_UPTIME, currentUptime)
.apply()
false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment