Android Application
Kotlin 브로드캐스트 리시버 : 배터리 상태를 표시
작성자 임베디드코리아
작성일23-10-23 23:09
조회1,331회
댓글0건
Kotlin 브로드캐스트 리시버(Broadcast Receiver,BR) : 배터리 상태를 표시
배터리 상태가 변할 때마다 충전 상태 이미지와 배터리 상태를 출력
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/ivBattery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/battery_0"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"/>
<EditText
android:id="@+id/edtBattery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffdab9"
android:textColor="#000000"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:enabled="false" />
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var ivBattery: ImageView
lateinit var edtBattery: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "배터리 상태 체크"
ivBattery = findViewById<ImageView>(R.id.ivBattery)
edtBattery = findViewById<EditText>(R.id.edtBattery)
}
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("전원 연결 : 안됨")
BatteryManager.BATTERY_PLUGGED_AC -> edtBattery.append("전원 연결 : 어댑터 연결됨")
BatteryManager.BATTERY_PLUGGED_USB -> edtBattery.append("전원 연결 : USB 연결됨")
}
}
}
}
override fun onPause() {
super.onPause()
unregisterReceiver(br)
}
override fun onResume() {
super.onResume()
var iFilter = IntentFilter()
iFilter.addAction(Intent.ACTION_BATTERY_CHANGED)
iFilter.addAction(Intent.ACTION_SCREEN_ON)
registerReceiver(br, iFilter)
}
}
------------------------------------------------------------------------------------
themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.BroadCastBattery" parent="Theme.AppCompat.Light">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.BroadCastBattery" parent="Base.Theme.BroadCastBattery" />
</resources>