My first application on Android: BatteryReporter

2 minute read


From a while I wanted to learn how to develop Android applications and when I saw that open a course at Coursera for developing Android appslications so I took the chance to learn. I must confess that it not  a piece of cake, I mean, it just not “copy and paste” code (although you can find example codes out there) and you must learn new concepts, because although you have programmed before (even Java) now you must learn how to do it the “Android’s way”.

After following the video lectures, read and read a lot, I build my first application, which in deed is part of the first assignment for the course. It’s a simple application which reads the device’s battery, and reports if it is charging or discharging, as well as the percentage of charge of the device.

The code (

Here you can take a look to the code for the application:

package com.lgallardo.batteryreporter;

import android.os.BatteryManager;
import android.os.Bundle;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

 protected void onCreate(Bundle savedInstanceState) {

 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(, menu);
 return true;

public void getStatus(View view) {

TextView statusValueTextView, chargingValueTextView, levelValueTextView;
 ImageView iconImageView;

 String charging = "";

int level, scale;
 float batteryPct;

// Get resources reference
 Resources res = getResources();

// Get values TextViews
 statusValueTextView = (TextView) findViewById(;
 chargingValueTextView = (TextView) findViewById(;
 levelValueTextView = (TextView) findViewById(;

 // Get ImageView (icon)

 iconImageView = (ImageView) findViewById(;

 // Get battery's status
 IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
 Intent batteryStatus = registerReceiver(null, ifilter);

// Check if the battery is charging or is charged?
 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
 boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
 || status == BatteryManager.BATTERY_STATUS_FULL;

// Update UI status

// Update UI charging
 if (isCharging) {
 charging = res.getString(R.string.yes);

// Get charging method
 int chargePlug = batteryStatus.getIntExtra(
 BatteryManager.EXTRA_PLUGGED, -1);
 boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
 // boolean acCharge = chargePlug ==
 // BatteryManager.BATTERY_PLUGGED_AC;

if (usbCharge) {
 charging = charging + " " + res.getString(R.string.usb);
 } else {
 charging = charging + " " + res.getString(;


} else {
 charging = res.getString(;


// Update UI level
 level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
 scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
 batteryPct = 100 * level / (float) scale;




Leave a Comment