8

When the battery on my Android device dies what methods in the Activity and Fragment classes (if any) are called during the "Powering Off" stage of the device?

Also, if a user is currently looking at a screen in my app and they hold the power button and choose switch off, do the events called/not called coincide with when the battery is depleted and shuts down automatically?

OnPause?

OnStop?

OnDestroy?

OnDetach?

Bonus: Will I have enough time to save a small amount of data to a web server?

To clarify "dies" when the device's battery is 'completely' dead, accepts no more input and a message box/loading screen pops up on the screen stating "Powering Off". Shortly there after the device switches off.

I just need enough time to save a forms state before the phone switches off, I have a strategy to clean the saved data should the phone not switch off, but I want to get as close to the phone switching off as possible (any more than a minute is pointless really).

Smithy
  • 2,170
  • 6
  • 29
  • 61
  • 1
    none of them. You need to use a broadcast receiver for this. – Rohit5k2 Jan 08 '15 at 17:10
  • 1
    @Rohit5k2: `none of them` source for that affirmation? (we are talking about a controlled power off, not a sudden battery removal) – njzk2 Jan 08 '15 at 17:13

2 Answers2

5

onDestroy is called on everything when the battery reaches 0.5%

EDIT: There is no specified time that you have to do anything in the shutdown process resulting from low/dead battery, that would be dependent on the specific phone battery and not the system, so you may have enough time to save data to a web server on some phones but not others. Experimentally, I have only been able to write a short line to a file I was already writing to before onDestroy was called and nothing more.

John
  • 92
  • 7
  • 1
    do you have source to back that affirmation and that number? – njzk2 Jan 08 '15 at 17:14
  • 2
    https://android.googlesource.com/platform/frameworks/base/+/625239a05401bbf18b04d9874cea3f82da7c29a1/services/java/com/android/server/BatteryService.java – John Jan 08 '15 at 17:18
  • 0.5% is the mark to round to 0 in battery handling so shutdownIfNoPower() will be called in the link I just posted – John Jan 08 '15 at 17:19
  • @John Do you have any information showing how much time / what I am able to do in the event of the device powering off? – Smithy Jan 08 '15 at 17:34
  • 1
    code says `MAX_BROADCAST_TIME = 10*1000` which is how much is waited when the broadcast is sent. (but no network should be done there, though). That is also the time given to the ActivityManager to shutdown. (See ShutdownThread). – njzk2 Jan 08 '15 at 17:59
  • @njzk2 Do this mean I need to go with Rohit5k2 to be able to persist my data to a web server? – Smithy Jan 08 '15 at 18:04
  • 1
    @Smithy The BATTERY_LOW is indeed an intent that is supposed to be sent before the device needs to shutdown. It does not mean that the device is going to be powered off, though. – njzk2 Jan 08 '15 at 18:06
  • @njzk2 I just need enough time to save a forms state before the phone switches off, I have a strategy to clean the saved data should the phone not switch off, but I want to get as close to the phone switching off as possible (any more than a minute is pointless really). – Smithy Jan 08 '15 at 18:21
  • 1
    @Smithy: then `BATTERY_LOW` does not really help you because it occurs at I think ~4%, at which point the user is notified and is likely to charge the device. (unless you also detect that too, and send a rollback) – njzk2 Jan 08 '15 at 18:26
  • 1
    @Smithy: I think the shutdown broadcast gives you enough time to save the data, but sending it to a server a/ would occur on the main thread and b/ could fail anyway. I would save the data and send it when the phone turns on again (but that may not suit your needs) – njzk2 Jan 08 '15 at 18:28
  • No it's there so the user can switch device and continue from where they left off. Thank you so much though :) – Smithy Jan 08 '15 at 18:36
2

The methods you have mentioned is activity life cycle callback, none of them will be called when battery is low. You need to use a broadcast receiver for this

See this How to detect when the Battery's low : Android?

Community
  • 1
  • 1
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • What is the last event I will receive before the device actually powers down? and roughly, how much time would I have before the phone automatically powers down? – Smithy Jan 08 '15 at 18:06
  • It would trigger the receiver on each change. You can take the action when you see fit. – Rohit5k2 Jan 08 '15 at 18:24