バッテリーの状態を表示するAndroidアプリ

adakoda.com - 
↑ここを参考に。

package jp.throwordie.batterytool;

import jp.throwordie.batterytool.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

public class BatteryActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  @Override
  protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(broadcastReceiver, filter);
  }

  @Override
  protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
  }

  private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
        int status = intent.getIntExtra("status", 0);
        int health = intent.getIntExtra("health", 0);
        boolean present = intent.getBooleanExtra("present", false);
        int level = intent.getIntExtra("level", 0);
        int scale = intent.getIntExtra("scale", 0);
        int icon_small = intent.getIntExtra("icon-small", 0);
        int plugged = intent.getIntExtra("plugged", 0);
        int voltage = intent.getIntExtra("voltage", 0);
        int temperature = intent.getIntExtra("temperature", 0);
        String technology = intent.getStringExtra("technology");

        String statusString = "";

        switch (status) {
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
          statusString = "unknown";
          break;
        case BatteryManager.BATTERY_STATUS_CHARGING:
          statusString = "charging";
          break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
          statusString = "discharging";
          break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
          statusString = "not charging";
          break;
        case BatteryManager.BATTERY_STATUS_FULL:
          statusString = "full";
          break;
        }

        String healthString = "";

        switch (health) {
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
          healthString = "unknown";
          break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
          healthString = "good";
          break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
          healthString = "overheat";
          break;
        case BatteryManager.BATTERY_HEALTH_DEAD:
          healthString = "dead";
          break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
          healthString = "voltage";
          break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
          healthString = "unspecified failure";
          break;
        }

        String acString = "";

        switch (plugged) {
        case BatteryManager.BATTERY_PLUGGED_AC:
          acString = "plugged ac";
          break;
        case BatteryManager.BATTERY_PLUGGED_USB:
          acString = "plugged usb";
          break;
        }

        TextView textView1 = (TextView) findViewById(R.id.TextView01);

        StringBuffer sb = new StringBuffer();
        sb.append("STATUS: " + statusString + "\n");
        sb.append("HEALTH: " + healthString + "\n");
        sb.append("PLESENT: " + String.valueOf(present) + "\n");
        sb.append("LEVEL: " + String.valueOf(level) + "\n");
        sb.append("SCALE: " + String.valueOf(scale) + "\n");
        sb.append("ICON_SMALL: " + String.valueOf(icon_small) + "\n");
        sb.append("PLUGGED: " + acString + "\n");
        sb.append("VOLTAGE: " + String.valueOf(voltage) + "\n");
        sb.append("TEMPERATURE: " + String.valueOf(temperature) + "\n");
        sb.append("TECHNOLOGY: " + technology + "\n");

        textView1.setText(sb.toString());
      }
    }
  };
}