0

Is there a way to get the last Shutdown time using Android API or any Android log?

I know I can use ACTION_SHUTDOWN event with a BroadcastReceviver to store the date of the shutdown, but I just want the time when the last shutdown occurred and I want to treat this event when the phone starts.

Does the API provide this somehow?

learning_frog
  • 305
  • 1
  • 3
  • 14

1 Answers1

1

Write ACTION_SHUTDOWN and BOOT_COMPLETED broadcast receiver

In ACTION_SHUTDOWN broadcast receiver you can save the current time to shared preference like this

SharedPreferences sp = context.getSharedPreferences("LastShutDown", context.MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putString("LastShutDownTime", currentTimeValue);
et.commit();

In BOOT_COMPLETED broadcast receiver(it will call when device starts or boot up) you can read last shutdown time from SharedPreferences

SharedPreferences sp = context.getSharedPreferences("LastShutDown", context.MODE_PRIVATE);
String lastShutDownTime=sp.getString("LastShutDownTime",null);
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37