var br: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
var action = intent.action
if (action == Intent.ACTION_BATTERY_CHANGED) {
var remain = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
edtBattery.setText("현재 충전량 : $remain %\n")
if (remain >= 90)
ivBattery.setImageResource(R.drawable.battery_100)
else if (remain >= 70)
ivBattery.setImageResource(R.drawable.battery_80)
else if (remain >= 50)
ivBattery.setImageResource(R.drawable.battery_60)
else if (remain >= 10)
ivBattery.setImageResource(R.drawable.battery_20)
else
ivBattery.setImageResource(R.drawable.battery_0)
var plug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)
when (plug) {
0 -> edtBattery.append("전원 연결 : 안됨\n")
BatteryManager.BATTERY_PLUGGED_AC -> edtBattery.append("전원 연결 : 어댑터 연결됨\n")
BatteryManager.BATTERY_PLUGGED_USB -> edtBattery.append("전원 연결 : USB 연결됨\n")
}
// 배터리 상태 출력
var status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0)
when (status) {
BatteryManager.BATTERY_STATUS_CHARGING -> Toast.makeText(applicationContext, "배터리 상태: 현재 충전중임",
Toast.LENGTH_SHORT).show()
BatteryManager.BATTERY_STATUS_NOT_CHARGING -> Toast.makeText(applicationContext,
"배터리 상태: 현재 충전중 아님", Toast.LENGTH_SHORT).show()
BatteryManager.BATTERY_STATUS_FULL -> Toast.makeText(applicationContext,
"배터리 상태: 충전 100% 완료", Toast.LENGTH_SHORT).show()
BatteryManager.BATTERY_STATUS_DISCHARGING -> Toast.makeText(applicationContext, "배터리 상태: 방전됨", Toast.LENGTH_SHORT)
.show()
else -> Toast.makeText(applicationContext,
"배터리 상태: 상태 알 수 없음", Toast.LENGTH_SHORT).show()
}
}
}
}
}