What Android methods are called when battery dies?
Asked Answered
Q

2

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).

Quadri answered 8/1, 2015 at 17:9 Comment(2)
none of them. You need to use a broadcast receiver for this.Suddenly
@Rohit5k2: none of them source for that affirmation? (we are talking about a controlled power off, not a sudden battery removal)Chlorophyll
T
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.

Toponym answered 8/1, 2015 at 17:14 Comment(11)
do you have source to back that affirmation and that number?Chlorophyll
android.googlesource.com/platform/frameworks/base/+/…Toponym
0.5% is the mark to round to 0 in battery handling so shutdownIfNoPower() will be called in the link I just postedToponym
@Toponym Do you have any information showing how much time / what I am able to do in the event of the device powering off?Quadri
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).Chlorophyll
@Chlorophyll Do this mean I need to go with Rohit5k2 to be able to persist my data to a web server?Quadri
@Quadri 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.Chlorophyll
@Chlorophyll 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).Quadri
@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)Chlorophyll
@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)Chlorophyll
No it's there so the user can switch device and continue from where they left off. Thank you so much though :)Quadri
S
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?

Suddenly answered 8/1, 2015 at 17:12 Comment(2)
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?Quadri
It would trigger the receiver on each change. You can take the action when you see fit.Suddenly

© 2022 - 2024 — McMap. All rights reserved.