Search code examples
androidandroid-alarms

Timestamp at which phone turned off?


For Android devices, is there any way to find out at what time ( or timestamp ) the device turned off?


Solution

  • I dont think there's any predefined support for that. However, its easy to get this done with your custom logic. All, you need to do is to define a BroadcastReceiver which listens for intent android.intent.action.ACTION_SHUTDOWN. Once it receives the intent, simply save the current Date in SharedPreferences, SQLite or where ever you want. Later, the phone is booted, read the saved value to know the estimated time when the phone was shutdown. For example:

    receiver code:

    public class ShutdownReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //save the date here
        }
    }
    

    and in AndroidManifest.xml

    <receiver android:name=".ShutdownReceiver">
      <intent-filter>
        <action android:name="android.intent.action.ACTION_SHUTDOWN" />
      </intent-filter>
    </receiver>