Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Studio Battery Checker 2-2
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/* 省略 */
if (action != null && action.equals(Intent.ACTION_BATTERY_CHANGED)) {
// バッテリーの状態
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
String message = "";
switch (status) {
case BatteryManager.BATTERY_STATUS_FULL:
message = "充電完了";
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
message = "充電中";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
message = "放電中";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
message = "充電していません";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
message = "不明";
break;
}
statusLabel.setText(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment