[Android] BroadcastReceiver

leeehaΒ·2022λ…„ 11μ›” 7일
0
post-thumbnail

λΈŒλ‘œλ“œμΊμŠ€νŠΈ λ¦¬μ‹œλ²„

  • 이벀트 λͺ¨λΈλ‘œ μ‹€ν–‰λ˜λŠ” μ»΄ν¬λ„ŒνŠΈ (μ—¬κΈ°μ„œ 이벀트 λͺ¨λΈμ€ μœ μ € μ΄λ²€νŠΈκ°€ μ•„λ‹ˆλΌ, μ‹œμŠ€ν…œμ˜ νŠΉμ • 상황을 μ˜λ―Έν•¨.)
  • BroadcastReceiverλ₯Ό 상속받은 클래슀 μž‘μ„±
class MyReceiver: BroadcastReceiver() {
	override fun onReceive(context: Context, intent: Intent){
    	// ... 
    }
}

λΈŒλ‘œλ“œμΊμŠ€νŠΈ λ¦¬μ‹œλ²„ 등둝

  • μ‹œμŠ€ν…œμ—μ„œ 생λͺ…μ£ΌκΈ°λ₯Ό κ΄€λ¦¬ν•˜λŠ” μ»΄ν¬λ„ŒνŠΈλ“€μ€ AndroidManifest.xml νŒŒμΌμ— λ“±λ‘ν•΄μ€˜μ•Ό ν•œλ‹€. (name은 ν•„μˆ˜ 속성)
<receiver
  android:name=".MyReceiver"
  android:enabled="true"
  android:exported="true">
</receiver> 
  • 그런데, λΈŒλ‘œλ“œμΊμŠ€νŠΈ λ¦¬μ‹œλ²„λŠ” λ§€λ‹ˆνŽ˜μŠ€νŠΈ νŒŒμΌμ— λ“±λ‘μ‹œν‚€μ§€ μ•Šκ³  μ½”λ“œμ—μ„œ ν•„μš”ν•œ μˆœκ°„μ— λ™μ μœΌλ‘œ 등둝할 수 μžˆλ‹€.
  • unregisterReceiver() ν•¨μˆ˜λ‘œ 등둝 ν•΄μ œ
val receiver = object: BroadcastReceiver(){
	override fun onReceive(context: Context?, intent: Intent?) {
    	// ... 
    }
}
val filter = IntentFilter("ACTION_RECEIVER") 
registerReceiver(receiver, filter) // λ™μ μœΌλ‘œ μ‹œμŠ€ν…œμ— 등둝 

// ...

unregisterReceiver(receiver) // 등둝 ν•΄μ œ 

λΈŒλ‘œλ“œμΊμŠ€νŠΈ λ¦¬μ‹œλ²„ μ‹€ν–‰

  • sendBroadcast() ν•¨μˆ˜λ‘œ λ¦¬μ‹œλ²„ μ‹€ν–‰ (κ²°κ΅­ 이 ν•¨μˆ˜λŠ” λΈŒλ‘œλ“œμΊμŠ€νŠΈλ₯Ό μ‹€ν–‰ν•˜κΈ° μœ„ν•œ μΈν…νŠΈλ₯Ό μ‹œμŠ€ν…œμ— λ°œμƒμ‹œν‚€λŠ” ν•¨μˆ˜κ°€ λœλ‹€.)
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>

슀크린 ON/OFF

  • 슀크린이 ON/OFF λ˜λŠ” μˆœκ°„μ— λΈŒλ‘œλ“œμΊμŠ€νŠΈ λ¦¬μ‹œλ²„ μ‹€ν–‰
  • μ½”λ“œμ—μ„œ registerReceiver() ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄ λ™μ μœΌλ‘œ λ“±λ‘ν•΄μ•Όλ§Œ μ‹€ν–‰ κ°€λŠ₯
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)
        }
    }
}

profile
μŠ΅κ΄€μ΄ 될 λ•ŒκΉŒμ§€ πŸ“

0개의 λŒ“κΈ€