Before I share my source code for Battery Status Application for Android. Below are the tools that you need to develop an Android Application or Widget:
- Android Development Environment - eclipse
- Android SDK
- Android Virtual Device Manager
For this Battery Status Application, below is the general outline:
- The layout is using ListView as the main and TextView for the individual row.
- Upon starting the application, a thread will be created to pull the battery info in 1-second interval. The battery info is being pulled using the BroadcastReceiver.
Below are the code for layout file "list_item.xml":
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:clickable="false" />
Below are the code for the main class:
public class BatteryStatusActivity extends ListActivity {
/** Called when the activity is first created. */
private String[] strText = new String[] {"Battery Level", "Voltage", "Status"};
private int voltage = 0;
private boolean trun = true;
private Handler myHandler = new Handler();
private Runnable myRun = new Runnable() {
public void run() {
updateNow();
}
};
// using Thread to keep the process running
private Thread myThread = new Thread() {
public void run () {
do {
batteryLevelUpdate();
myHandler.post(myRun);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (trun);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getApplicationContext(), "email", 2000).show();
startMonitor();
}
@Override
public void onDestroy() {
trun = false;
super.onDestroy();
}
private void startMonitor() {
myThread.start();
}
private void updateNow() {
ListView thisListView = getListView();
thisListView.setEnabled(false);
ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText);
thisListView.setAdapter(myList);
}
private void batteryLevelUpdate() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int onplug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
boolean onUSB = onplug == BatteryManager.BATTERY_PLUGGED_USB;
boolean onAC = onplug == BatteryManager.BATTERY_PLUGGED_AC;
String strStatus = "Charging on ";
if (isCharging && onUSB)
strStatus += "USB";
else if (isCharging && onAC)
strStatus += "AC Power";
else
strStatus = "Battery Discharging";
strText[0] = "Battery Level: " + Integer.toString(level) + "%";
strText[1] = "Voltage: " + Integer.toString(voltage) + "mV";
strText[2] = strStatus;
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
}
Take note that:
If you want the ListView is non-clickable & non-scrollable, you need to set the following:
ListView thisListView = getListView();
thisListView.setEnabled(false);
If you want the ListView is non-clickable but still scrollable, you need to set the following:
ArrayAdapter<String> myList = new ArrayAdapter<String>(this, R.layout.list_item, strText) {
public boolean isEnabled(int position)
{
return false;
}
};
That's all for now.
*How did I place the source code here? visit http://codeformatter.blogspot.com/
:: aerobrainTech ::


0 comments:
Post a Comment