0

Does Android provide a simple way to find out programmatically in my Java app when the last datetime the system was turned off was? I'm not looking for when it was last turned on, but last turned off.

I've found lots of information for when it was last turned on. But nothing useful to me for when it was last turned off.

NOTE: Setting up a broadcast listener for system shutdowns is not going to work for my particular use-case. This is because when my app starts up I need to know when the device was last shut down, even if my app (at the time) had been forced-off by the user. It won't receive broadcasts if it has been forced off.


EDIT: I tried getting logcat records, since a comment in another thread indicated that even without rooting the device or running a system app, some (not many) system logs would be available in addition to app logs. However, the logs that I was able to see based on this code only start when the device itself starts. So that was no help.

gcdev
  • 1,406
  • 3
  • 17
  • 30
  • I am not 100% sure. But this can be in help https://developer.android.com/reference/android/app/admin/SecurityLog – Hasan Apr 08 '22 at 21:41
  • how about writing a background service which always runs, it can store last power off. then your App can query that data , when ever you want. – SoftwareGuy Apr 08 '22 at 21:52
  • @SoftwareGuy wouldn't the background service stop if the user forces my app to stop? I need something that will reliably tell me the last shutdown datetime even if my app wasn't running at all. – gcdev Apr 08 '22 at 22:18
  • No, you can keep the service running always. Even you can run it in separate process. https://stackoverflow.com/questions/15758980/android-service-needs-to-run-always-never-pause-or-stop – SoftwareGuy Apr 08 '22 at 22:24
  • 1
    You absolutely can NOT programmatically force a process to continue running indefinitely. Imagine if that were true. It would be a malware developer's dream come true. – zen_of_kermit Apr 09 '22 at 00:03
  • Just FYI, background services (even sticky ones) do stop when the user forces the app to stop. At least, that's what my own tests have shown. – gcdev Apr 11 '22 at 18:11

0 Answers0