class MyReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent){
// ...
}
}
AndroidManifest.xml
νμΌμ λ±λ‘ν΄μ€μΌ νλ€. (nameμ νμ μμ±) <receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
</receiver>
val receiver = object: BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent?) {
// ...
}
}
val filter = IntentFilter("ACTION_RECEIVER")
registerReceiver(receiver, filter) // λμ μΌλ‘ μμ€ν
μ λ±λ‘
// ...
unregisterReceiver(receiver) // λ±λ‘ ν΄μ
val intent = Intent(this, MyReceiver::class.java)
sendBroadcast(intent)
AndroidManifest.xml
νμΌμ μμ€ν
μμ λμ°λ μΈν
νΈ μ λ³΄λ‘ intent-filterλ₯Ό ꡬμ±ν΄ λ±λ‘νλ€. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
val filter = IntentFilter(Intent.ACTION_SCREEN_ON).apply {
addAction(Intent.ACTION_SCREEN_OFF)
}
registerReceiver(receiver, filter)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c68">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
</receiver>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MyReceiver.kt
package com.tutorial.c68
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.e("haeun", "receiver~~~~")
}
}
MainActivity.kt
package com.tutorial.c68
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val resultView = findViewById<TextView>(R.id.resultView)
val button = findViewById<Button>(R.id.button)
registerReceiver(null,
IntentFilter(Intent.ACTION_BATTERY_CHANGED))?.apply {
var isCharging = "Not Plugged"
when(getIntExtra(BatteryManager.EXTRA_STATUS, -1)){
BatteryManager.BATTERY_STATUS_CHARGING -> {
when(getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)){
BatteryManager.BATTERY_PLUGGED_USB -> {
isCharging = "USB Plugged"
}
BatteryManager.BATTERY_PLUGGED_AC -> {
isCharging = "AC Plugged"
}
}
}
}
val level = getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = getIntExtra(BatteryManager.EXTRA_SCALE, -1)
val batteryPct = level / scale.toFloat() * 100
resultView.text = "${isCharging}, ${batteryPct}%"
}
button.setOnClickListener {
val intent = Intent(this, MyReceiver::class.java)
sendBroadcast(intent)
}
}
}