First I will briefly show how to react to the battery percentage change in your application, and then show how to change the battery level in the android emulator (AVD).
Reacting to the battery level change in your program
We need to register our application to receive the intent when the battery level information is changed, we do this by adding the following line into our onCreate method of our activity (or some other instance):
registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
This will run mBatInfoReceiver when the battery level is changed, so we now need to create this method in our activity (or some other instance). The function creates a broadcast receiver to handle the information when the intent is sent for the battery status changing.
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent intent) { int level = intent.getIntExtra("level", 0); // TODO: Preform action based upon battery level } };
Changing the battery level in AVD
First we need to find the port so that we can address the emulator, this will be shown in the titlebar of the AVD.
Zooming in:

Using the port from the AVD found above we can use telnet to control certain aspects of the avd see this android reference for a full reference.
telnet localhost 5554 Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Android Console: type 'help' for a list of commands OK help Android console command help: help|h|? print a list of commands event simulate hardware events geo Geo-location commands gsm GSM related commands kill kill the emulator instance network manage network settings power power related commands quit|exit quit control session redir manage port redirections sms SMS related commands avd manager virtual device state window manage emulator window try 'help' for command-specific help OK
Some more information on the power commands:
help power allows to change battery and AC power status available sub-commands: display display battery and charger state ac set AC charging state status set battery status present set battery present state health set battery health state capacity set battery capacity state
So now you can change the battery level in the avd using power capacity percentage
power capacity 100 OK power capacity 99 OK power capacity 60 OK power capacity 20 OK power capacity 0 OK
As you change the battery capacity you should notice that your AVD is responding, just by looking at the battery indicator in the status bar, and your program will react if you have replaced the //TODO: code with something that reacts when the onReceive is called.
hi,
Can you tell me where i should enter this “telnet localhost 5554”, in the command prompt I moved tools directory and gave this command, but i am getting ‘telnet’ not supported 🙁 Help me in this.
Regards,
anu
@Anu, It depends what operating system you are using. I was under the impression windows has telnet by default (perhaps you don’t have access to it?).
I was having trouble getting telnet to work. I would receive the error “‘telnet’ is not recognized as an internal or external command, operable program or batch file”. To fix this issue, go to Add/Remove Programs, Turn Windows Features on and off, then check Telnet Client. This should fix the problem.
Awesome.. works great:)
Thanks for the information. It works but I have a problem. When I connect via telnet, my emulator gets unresponsive. It changes the battery status as I expect, but then I cannot do anything else in emulator, it gets frozen. I use Ubuntu 12.04 and Android 4.0.3 emulator with Intel x86 system image. Do you know why might this be happening?
Thanks.
Hi,
I tried it but the emulator stops working on writing the power capacity 100 command. I tried all other commands and it works fine. Only the power command causes the emulator to stop working. Any help would be appreciated.
Thank you
telnet is not enabled by default in Windows 7.
To enable it, follow https://social.technet.microsoft.com/wiki/contents/articles/910.how-to-enable-telnet-client-in-windows-7.aspx
If you want, you can use my friendly application to send commands to AVD.
Android Graphical Console.
https://github.com/marcusbecker/AndroidGC/raw/master/dist.zip
in telnet, you may need to use
power capacity XX%
to make it work.
for example
power capacity 25%
(tested in windows 8)
I enabled hw.battery=yes in .android/avd/config file and it works so nice
Thank you very much for this tip
[…] Changing the battery percentage in an AVD (android virtual … – First I will briefly show how to react to the battery percentage change in your application, and then show how to change the battery level in the android emulator (AVD)…. […]
[…] power capacity 100 is what you should set for emulator , no we do not want to see anything less than that […]
[…] http://www.stealthcopter.com/blog/2010/07/changing-the-battery-percentage-in-an-avd-android-virtual-… […]