14

Can anyone tell me how can I simulate low battery in my rooted device?

I gone through this question but did not find much information on this.

Community
  • 1
  • 1
AndroDev
  • 3,236
  • 8
  • 35
  • 49

6 Answers6

9

With the Android Emulator's Extended Controls, it's now possible to set the battery level with a GUI slider called "Charge Level."

To access this, start the emulator. Then click the "..." at the bottom of the settings panel (which hovers to the right of the emulator).

enter image description here

This opens the Extended Controls panel which contains a "Battery" menu item:

enter image description here

The Charge Level slider goes from 0 to 100%. You can also simulate failed/overheated/etc physical battery, and being off the charger by adjusting the other drop down controls.

albert c braun
  • 2,650
  • 1
  • 22
  • 32
7

Use the following commands

To simulate the device being unplugged:

adb shell dumpsys battery unplug

To test how the device behaves under low power conditions:

adb shell settings put global low_power 1

Once you have finished your testing, you can undo your manual device settings with this command:

adb shell dumpsys battery reset
Gulzar Bhat
  • 1,255
  • 11
  • 13
6

simulating low battery is not provided by default as far as i know.(if you are using eclipse as im). But if you are in linux , u can use telnet to connect to ur localhost emulator and perform 'Power' actions.(not used them maybe u can give a try) http://handycodeworks.com/?p=46

But u dont have to go through that process. Just register a broadcast receiver for ACTION_BATTERY_LOW, and it is guaranteed to be called in low battery scenarios.

Hope it helps

EDIT:

here is the direct answer (assuming that ur running windows).

Enable 'telnet' on windows if you havent already.

Control panel-->programs-->under 'programs and features' select 'turn windows features on or off'-->it opens a new window select 'telenet client' and click on OK.

start command prompt with admin rights(in AllPrograms search for 'cmd' and right click on it and select run as admin).

then use this commands

1)telnet localhost 5554 //where 5554 is your emulator id, which is displayed top left   corner of ur emulator
2)power capacity 10   //set the battery level to 10%
3)power ac off    //turns off charging mode

Now you can see a low battery dialog in emulator.

oops.objective
  • 1,264
  • 1
  • 8
  • 8
  • 1
    Thank you for answer but actually I am testing one app and my scenario is to be tested in low battery condition. So every time I have to wait for device battery to become low. Your solution includes app changes which I do not want. – AndroDev Apr 30 '13 at 10:06
  • @oops.objective even though I'm able to turn off charging mode and lower battery level, I still can't get notified via my receiver . Any tip? – MatheusJardimB Jun 05 '15 at 18:12
  • 1
    After step (1) I needed to do the `auth` command, as described in http://stackoverflow.com/questions/40700725/android-terminal-telnet-missing-commands-receiving-this-error-ko-unknown-co. After that it worked exactly as described here, and I was even able to trace it in the AVD emulator with the debugger, into my BATTERY_LOW BroadcastReceiver! More info: Windows 10, AndroidStudio. I did need to turn on Telnet Client as described, but I did not need to run it in Administrator mode. Thanks! – Stephen Hosking Feb 06 '17 at 21:49
1

Is the battery removable? If so, get a spare battery and keep it in a low charge state for your low battery testing.

paulkayuk
  • 1,052
  • 1
  • 8
  • 15
  • Yes battery is removable but what you are suggesting is not the answer of my question. I want to simulate low battery condition in my android device using something like adb commands. – AndroDev Apr 30 '13 at 10:38
1

The link from @user2240369 actually leads you to the right answer. You should do this:

telnet localhost 5554 #or wahtever port you are using
power capacity 60
EyalBellisha
  • 1,874
  • 19
  • 28
0

You can make an ActivityInstrumentationTestCase2 and call the various onPause, onStop, and onDestroy methods to simulate a low memory situation only the onPause is guaranteed to be called:

In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState(Bundle) is used to save per-instance state in the activity and this (onPause) method is used to store global persistent data (in content providers, files, etc.)

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • Thank you for answer but actually I am testing one app and my scenario is to be tested in low battery condition. So every time I have to wait for device battery to become low. Your solution includes app changes which I do not want. – AndroDev Apr 30 '13 at 10:06
  • I have but this I can do by hardcoding some values too. I do not want to hardcode values. That is why I asked this. – AndroDev Apr 30 '13 at 10:34
  • @AndroDev You don't modify the actual activity using this, you make a test for the particular `Activity` in which you can call the life cycle events to simulate a low battery situation. Read : http://developer.android.com/tools/testing/activity_testing.html – Emil Davtyan Apr 30 '13 at 10:49