AVD

Changing the battery percentage in an AVD (android virtual device) emulator

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
			}
		  };

(reference)

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.

Froyo running in an AVD (android emulator)

Froyo running in an AVD (android emulator)

Zooming in:

AVD title shows the port "5554:froyo"

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.

Read More