いまさら?って感じもするけどAndroidのバッテリー情報を取得する方法です。
いってみよ!
BroadcastReceiverをnewするメソッド
public static BroadcastReceiver statusReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String text = "取得してない"; if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { boolean present = intent.getBooleanExtra("present", false); int level = intent.getIntExtra("level", 0); int scale = intent.getIntExtra("scale", 0); int iconSmall = intent.getIntExtra("icon-small", 0); int health = intent.getIntExtra("health", 0); int voltage = intent.getIntExtra("voltage", 0); int temperature = intent.getIntExtra("temperature", 0); String technology = intent.getStringExtra("technology"); int status = intent.getIntExtra("status", 0); int plugged = intent.getIntExtra("plugged", 0); String statusText = "???"; switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: statusText = "Unknown"; break; case BatteryManager.BATTERY_STATUS_CHARGING: statusText = "Charging"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: statusText = "Discharging"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: statusText = "Not charging"; break; case BatteryManager.BATTERY_STATUS_FULL: statusText = "Full"; break; } String healthText = "???"; switch (health) { case BatteryManager.BATTERY_HEALTH_UNKNOWN: healthText = "Unknown"; break; case BatteryManager.BATTERY_HEALTH_GOOD: healthText = "Good"; break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: healthText = "Overheat"; break; case BatteryManager.BATTERY_HEALTH_DEAD: healthText = "Dead"; break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: healthText = "Voltage"; break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: healthText = "Failure"; break; } String pluggedText = "???"; switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: pluggedText = "AC"; break; case BatteryManager.BATTERY_PLUGGED_USB: pluggedText = "USB"; break; } text = "present 有無:" + present; text += "\n" + "level 残量:" + level; text += "\n" + "scale 残量の最大値:" + scale; text += "\n" + "health 健康状態:" + healthText; text += "\n" + "voltage 電圧(mV):" + voltage; text += "\n" + "temperature 温度(*0.1):" + temperature; text += "\n" + "technology:" + technology; text += "\n" + "status 充電状態:" + statusText; text += "\n" + "plugged 接続:" + pluggedText; text += "\n" + "icon_small アイコンID:" + iconSmall; } Log.d(text); } };
BroadcastReceiverの登録と解除
ActivityやFragmentのライフサイクルのほどよい箇所でブロードキャストを登録したり解除したりします。
addActionで「Intent.ACTION_BATTERY_CHANGED」をつけとく。
@Override public void onResume() { super.onResume(); // Regist Receiver IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); getActivity().registerReceiver(statusReceiver, filter); } @Override public void onPause() { super.onPause(); // Receiver unregist getActivity().unregisterReceiver(statusReceiver); }
FragmentだったのでgetActivity()つけてるけど、普通のActivityなら要らんです。
取得できる情報の「technology」ってなんなんだろ?
参考記事
残量などバッテリーの情報を取得する
http://techbooster.jpn.org/andriod/device/1586/
バッテリーの情報(Battery information)を取得するには
http://www.adakoda.com/android/000140.html