My first application on Android: BatteryReporter
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 (MainActivity.java)
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.app.Activity;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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(R.id.statusValue);
chargingValueTextView = (TextView) findViewById(R.id.chargingValue);
levelValueTextView = (TextView) findViewById(R.id.levelValue);
// Get ImageView (icon)
iconImageView = (ImageView) findViewById(R.id.imageView1);
// 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
statusValueTextView.setText(Integer.toString(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(R.string.ac);
}
iconImageView.setImageResource(R.drawable.charging);
} else {
charging = res.getString(R.string.no);
iconImageView.setImageResource(R.drawable.discharging);
}
chargingValueTextView.setText(charging);
// Update UI level
level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
batteryPct = 100 * level / (float) scale;
levelValueTextView.setText(Float.toString(batteryPct)+"%");
}
}
Leave a Comment