1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android】電池残量の取得【kotlin】

Posted at

起動時に、取得した端末の電池残量をTextViewに表示するようにします。

電池残量や充電中か否かなどをBatteryManagerクラスで取得できます。

    private val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { ifilter ->
        this.registerReceiver(null, ifilter)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textView: TextView = findViewById(R.id.text_view)

        val battery: Int? = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL,-1)

        textView.text = batteryPct.toString()
    }

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?