43

I need to know when the user powers off his/her phone. Are there any broadcasts (or similar) that notify when the user's phone is powered off?

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
BenIOs
  • 469
  • 2
  • 5
  • 8
  • 1
    @BenIOs: like your code won't run anymore, joking! maybe if you make your application write on a file or do a ajax call every n minute and analyze that after. – RageZ Feb 03 '10 at 06:33
  • There is probably some log written somewhere for that. I'd be very surprised if there wasn't. – Ritwik Bose Feb 03 '10 at 06:38
  • 3
    Very off-topic: The wording made me think of the BeOS function `is_computer_on()`: when the computer is on, it returns 1, otherwise the result is undefined. – bk1e Feb 03 '10 at 06:50

3 Answers3

69

You can use the ACTION_SHUTDOWN Intent which is broadcast when the phone is about to shutdown. The documentation says:

Apps will not normally need to handle this, since the foreground activity will be paused as well.

In other words, if you respond to all the lifecycle events for your Activity appropriately, there's no need to use this unless you really want to do something specific related to shutdown.

The ACTION_SHUTDOWN Intent was introduced in API Level 4, in other words it'll only be sent on phones running Android 1.6 or later.

You'll trap the Broadcast with a BroadcastReceiver. It will look something like this:

public class ShutdownReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Insert code here
    }

}

You'll also need an entry in your Manifest like the following:

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

Depending on what you're doing, another option would be to use the ACTION_BOOT_COMPLETED Intent which is sent when the phone is restarted.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • i did same as above but nothing happend...i added Log.v("Shutdown Event","Device shutting down"); but not able to do anything on this event, kindly give any suggestion..thanks – Sandeep Apr 03 '12 at 13:46
  • @DeepSan - I would make sure your Manifest entry points to the correct class. – David Webb Apr 03 '12 at 15:02
  • @DaveWebb- Sir, will you plz explain what is the mean of your comment, i did not get..will you plz tell what should i do to save shut down time in a text file... – Sandeep Apr 04 '12 at 04:16
  • @DeepSan The `android:name` in your `` entry must be the class name of the `BroadcastReceiver` you have implemented. http://developer.android.com/reference/android/content/BroadcastReceiver.html – David Webb Apr 04 '12 at 10:03
  • @DaveWebb- Hello sir, the issue i have been already resolve,there was small mistake in code. i fixed now everything i fine..thanks for your precious time and valuable ideas... – Sandeep Apr 06 '12 at 12:39
  • 2
    Is there any guarantee your code will be given enough time to complete using this? I.e. does the shutdown continue or will it wait/block for your receiver to finish doing whatever it needs to? – matt5784 Jul 02 '13 at 17:12
  • 1
    @matt5784 take a look at the source code to the shutdown process in this link. They give you 10 seconds to do what you need to do before shutting down the radio, unmounting the fs etc: https://github.com/android/platform_frameworks_base/blob/master/services/java/com/android/server/power/ShutdownThread.java – bsautner May 20 '14 at 20:14
  • 10 seconds is not really that great, you have no way of knowing who else is consuming cycles here so there's still no guarantee you will get a turn. Admittedly, 10s would probably be plenty in most cases, but without knowing for sure (that it is blocking on you or something) you can't really design around it. – matt5784 May 21 '14 at 00:26
20

In addition to ACTION_SHUTDOWN, you should add android.intent.action.QUICKBOOT_POWEROFF to your intent-filter.
ACTION_SHUTDOWN isn't always broadcast on some HTC devices (e.g. the Evo 4g).
To be more specific, if you choose Restart, ACTION_SHUTDOWN is broadcast, but if you choose Power Off, QUICKBOOT_POWEROFF is broadcast instead.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
Naliba
  • 856
  • 1
  • 8
  • 13
0

To expand on what Dave Webb said is the appropriate way to handle this:

You can override the Android Activity Lifecycle functions:

protected void onPause();
protected void onResume();

In most scenarios implementing these should be sufficient and will not require you to specifically handle the "power off" events.

There's plenty more information on the Android Website and in the answer to this lifecycle question.

Community
  • 1
  • 1
noelicus
  • 14,468
  • 3
  • 92
  • 111