バッテリーの充電情報を BatteryManager と Broadcast Intent のACTION_BATTERY_CHANGED を使って取得できます。また、Lollipop で追加された機能もあります。
2021.2.1
BateryManager
BatteryManagerはバッテリー情報を sticky Intent でブロードキャストしますが、sticky Intent なので BroadCast の登録をわざわざしなくてもよく、null を渡せばできます。
Ref: BatteryManager
Monitoring the Battery Level and Charging State
簡単に今の電池残量は下のようにして取得できます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//package your.package.name; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = this.registerReceiver(null, intentfilter ); int batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); Log.d("Battery","Level: " + batteryLevel); } } |
バッテリーレベルはログによると「91」です
1 |
2022-06-09 13:00:04.527 10393-10393/com.example.testbatterylevel D/Battery: Level: 91 |
ただ、正確には BatteryManager.EXTRA_SCALE で最大値を出して
level/maxLevel
のように計算しますが、大概Max は100の場合が多いようですのでそのまま%で扱えます。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
//package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private final StringBuilder sbuilder = new StringBuilder(); private Intent batteryStatus; private BatteryManager bManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); batteryStatus = this.registerReceiver(null, ifilter); if(batteryStatus != null) { baInfo(); } // 以降は Lollipop と機種によるサポートの違いあり bManager = (BatteryManager)this.getSystemService(Context.BATTERY_SERVICE); if(bManager != null){ batteryAgeing(); } TextView textView = findViewById(R.id.text_view); textView.setText(sbuilder); } private void baInfo(){ // Battery Level int level = batteryStatus.getIntExtra( BatteryManager.EXTRA_LEVEL, -1); sbuilder.append("Level: "); sbuilder.append(level); sbuilder.append("\n"); // Battery Health int bh = batteryStatus.getIntExtra( BatteryManager.EXTRA_HEALTH, -1); sbuilder.append("Health: "); sbuilder.append(batteryHealth(bh)); sbuilder.append("\n"); // icon ID int bi = batteryStatus.getIntExtra( BatteryManager.EXTRA_ICON_SMALL, -1); sbuilder.append("Icon: "); sbuilder.append(bi); sbuilder.append("\n"); // Battery plugged int bpl = batteryStatus.getIntExtra( BatteryManager.EXTRA_PLUGGED, -1); sbuilder.append("Plugged: "); sbuilder.append(batteryPluggCheck(bpl)); sbuilder.append("\n"); // Battery present or not boolean bpr = batteryStatus.getBooleanExtra( BatteryManager.EXTRA_PRESENT, false); sbuilder.append("Present: "); sbuilder.append(batteryPresent(bpr)); sbuilder.append("\n"); // scale int bsc = batteryStatus.getIntExtra( BatteryManager.EXTRA_SCALE, -1); sbuilder.append("Scale: "); sbuilder.append(bsc); sbuilder.append("\n"); // Battery status int bst = batteryStatus.getIntExtra( BatteryManager.EXTRA_STATUS, -1); sbuilder.append("Status: "); sbuilder.append(status(bst)); sbuilder.append("\n"); // Battery technology String bte = batteryStatus.getStringExtra( BatteryManager.EXTRA_TECHNOLOGY); sbuilder.append("Technology: "); sbuilder.append(bte); sbuilder.append("\n"); // temperature int btp = batteryStatus.getIntExtra( BatteryManager.EXTRA_TEMPERATURE, -1); sbuilder.append("Tempareture: "); sbuilder.append((float)btp/10); sbuilder.append(" ℃\n"); // Voltage int bv = batteryStatus.getIntExtra( BatteryManager.EXTRA_VOLTAGE, -1); sbuilder.append("Voltage: "); sbuilder.append((float)bv/1000); sbuilder.append(" [v]\n\n"); } private String batteryHealth(int bh){ String health = null; if(bh == BatteryManager.BATTERY_HEALTH_GOOD){ health = "GOOD"; } else if(bh == BatteryManager.BATTERY_HEALTH_DEAD){ health = "DEAD"; } else if(bh == BatteryManager.BATTERY_HEALTH_COLD){ health = "COLD"; } else if(bh == BatteryManager.BATTERY_HEALTH_OVERHEAT){ health = "OVERHEAT"; } else if(bh == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){ health = "OVER_VOLTAGE"; } else if(bh == BatteryManager.BATTERY_HEALTH_UNKNOWN){ health = "UNKNOWN"; } else if(bh == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){ health = "UNSPECIFIED_FAILURE"; } return health; } private String batteryPluggCheck(int bpl){ String plugged = "NOT PLUGGED"; if(bpl == BatteryManager.BATTERY_PLUGGED_AC){ plugged = "PLUGGED AC"; } else if(bpl == BatteryManager.BATTERY_PLUGGED_USB){ plugged = "PLUGGED USB"; } else if(bpl == BatteryManager.BATTERY_PLUGGED_WIRELESS){ plugged = "PLUGGED WIRELESS"; } return plugged; } private String batteryPresent(boolean bpr){ String present; if(bpr){ present = "PRESENT"; } else{ present = "NOT PRESENT"; } return present; } private String status(int bst){ String status = "?"; if(bst == BatteryManager.BATTERY_STATUS_CHARGING){ status = "CHARGING"; } else if(bst == BatteryManager.BATTERY_STATUS_DISCHARGING){ status = "DISCHARGING"; } else if(bst == BatteryManager.BATTERY_STATUS_FULL){ status = "FULL"; } else if(bst == BatteryManager.BATTERY_STATUS_NOT_CHARGING){ status = "NOT CHARGING"; } else if(bst == BatteryManager.BATTERY_STATUS_UNKNOWN){ status = "UNKNOWN"; } return status; } private void batteryAgeing(){ // RemainingCapacityRatio int remainingCapacityRatio = bManager.getIntProperty( BatteryManager.BATTERY_PROPERTY_CAPACITY); sbuilder.append("RemainingCapacityRatio: "); sbuilder.append(remainingCapacityRatio); sbuilder.append(" %\n"); // BatteryCapacityMicroAh int batteryCapacityMicroAh = bManager.getIntProperty( BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER); sbuilder.append("BatteryCapacityMicroAh: "); sbuilder.append(batteryCapacityMicroAh); sbuilder.append(" uAh\n"); // DischargingTimeSeconds int dischargingTimeSeconds = bManager.getIntProperty( BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE); sbuilder.append("DischargingTimeSeconds: "); sbuilder.append(dischargingTimeSeconds); sbuilder.append(" msec\n"); // Current int currentNow = bManager.getIntProperty( BatteryManager.BATTERY_PROPERTY_CURRENT_NOW); sbuilder.append("CurrentNow: "); sbuilder.append(currentNow); sbuilder.append(" uA\n"); // EnergyCounter long energyCounter = bManager.getLongProperty( BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER); sbuilder.append("EnergyCounter: "); sbuilder.append(energyCounter); sbuilder.append(" nWh\n\n"); } } |
Android機により一部の機能のみの実装だったりと機種次第のところがあります。いずれこれが実装されれば便利なアプリが作れそうです。
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:background="#dfe" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:textSize="20sp" android:textColor="#000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> |
基本的にバッテリー残量などの外部デバイスを使う場合は実機に依存する部分があることを前提にすべきですが、Android Studio 2.0からemulatorである程度確認することができます。
バッテリー残量やその他のステータスを変更して試せます。