このコードを使用すると、バッテリーに関する情報を取得できます。
private BroadcastReceiver battery_receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
boolean isPresent = intent.getBooleanExtra("present", false);
String technology = intent.getStringExtra("technology");
int plugged = intent.getIntExtra("plugged", -1);
int scale = intent.getIntExtra("scale", -1);
int health = intent.getIntExtra("health", 0);
int status = intent.getIntExtra("status", 0);
int rawlevel = intent.getIntExtra("level", -1);
int level = 0;
String temp=null;
Bundle bundle = intent.getExtras();
Log.i("BatteryLevel", bundle.toString());
if(isPresent)
{
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
String info = "Battery Level: " + level + "%\n";
info += ("Technology: " + technology + "\n");
info += ("Plugged: " + getPlugTypeString(plugged) + "\n");
info += ("Health: " + getHealthString(health) + "\n");
info += ("Status: " + getStatusString(status) + "\n");
info += ("Temp: "+getTempStatus(temp,intent)+"\n");
setBatteryLevelText(info + "\n\n" + bundle.toString());
}
else
{
setBatteryLevelText("Battery not present!!!");
}
}
};
private void registerBatteryLevelReceiver(){
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(battery_receiver, filter);
}