0
\$\begingroup\$

I want to pause my game when the battery warning on Android pops up. How do I do this? I have a method that pauses the game but how do I call it when the battery warning pops up?

\$\endgroup\$
1
\$\begingroup\$

Android does not give you that information if you are not looking for it as far as I know. onPause is called due to the fact that you get a pop-up on the screen, which means you are not currently running the GameActivity. You could stop there and design your game in a manner that when an external factor interrupts your activity you should pause the game when onResume is being called.

Also to answer the "How Can I monitor battery life", you should follow this tutorial:

http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

However pay attention to the bottom line since polling the battery life consumes mores battery than the actual usefulness of the information it gives you.

\$\endgroup\$
  • \$\begingroup\$ Battery warning on Android doesn't call onPause, that's the problem - the pop up appears over the game in progress and ruins it. \$\endgroup\$ – TimSim Jun 4 '14 at 20:16
  • \$\begingroup\$ Do you get the pop up from the OS? Or is it a toast message? \$\endgroup\$ – Cristina Jun 4 '14 at 20:26
  • \$\begingroup\$ Pop up from the OS. Does it call pause() and/or resume() methods in my game (implements Screen, InputProcessor, ApplicationListener)? \$\endgroup\$ – TimSim Jun 4 '14 at 20:26
  • \$\begingroup\$ As it should go when you get a pop up from the OS, your application will be puased => onPause being called by the OS. When you dismiss the popup the OS calls onResume. Look at the post people from Shot who gives out a link where you can find if you are doing it correctly or not for LibGDX. What I told you was how a pure Android Activity would behave. \$\endgroup\$ – Cristina Jun 4 '14 at 20:31
0
\$\begingroup\$

Look at this link: https://stackoverflow.com/a/21576506/1892346 . From what I know (I may be wrong) Android will handle pausing itself, the only job that you have to do is to handle Android callbacks (pause and resume methods).

\$\endgroup\$
  • \$\begingroup\$ I think I already have a call to my pause method in pause() and resume() but I can't be sure the pop-up warning triggers the pause() and resume() methods, I don't know how to test it except wait for my battery to run out \$\endgroup\$ – TimSim Jun 4 '14 at 20:30
  • \$\begingroup\$ You can test it via calling to yourself from different phone (onPause should be triggered then) or press the Home button, then when you are on the main screen, hold Home button and paused acivities should show. \$\endgroup\$ – Shot Jun 5 '14 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.