Created
December 10, 2024 06:01
-
-
Save logickoder/a7fa0674f04bb582d0198d07259a794a to your computer and use it in GitHub Desktop.
Check if an android device restarted without depending on BOOT_COMPLETED
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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