问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
```kotlin
package com.example.myapplication98
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.content.*
import android.widget.Button
import android.app.AlertDialog
import android.view.View
class MainActivity : AppCompatActivity() {
private var intLevel = 0
private var intScale = 0
private var mButton1: Button? = null
private var d: AlertDialog? = null
private val mBatInfoReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context,intent: Intent) {
val action = intent.action
if (Intent.ACTION_BATTERY_CHANGED == action) {
intLevel = intent.getIntExtra("level", 0)
intScale = intent.getIntExtra("scale", 100)
onBatteryInfoReceiver(intLevel, intScale)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mButton1 = findViewById<View>(R.id.Button) as Button
mButton1!!.setOnClickListener {
registerReceiver(mBatInfoReceiver,
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
)
}
}
fun onBatteryInfoReceiver(intLevel:Int, intScale:Int) {
val d = AlertDialog.Builder(this@MainActivity).create()
d.setTitle(R.string.str_dialog_title)
d.setMessage(resources.getString(R.string.str_dialog_body) + intLevel * 100 / intScale + "%")
d.setButton(resources.getString(R.string.str_button2),
DialogInterface.OnClickListener { dialog, which ->
unregisterReceiver(mBatInfoReceiver)
d.dismiss()
})
d.show()
}
}
<resources>
<string name="app_name">My Application98</string>
<string name="str_title">如何获取电池电量</string>
<string name="str_button1">按我取得电池电量</string>
<string name="str_button2">返回</string>
<string name="str_dialog_title">系统信息</string>
<string name="str_dialog_body">电池电量:</string>
</resources>
```kotlin
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Button"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2" />
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.387"
tools:ignore="DuplicateIds" />
</androidx.constraintlayout.widget.ConstraintLayout>