[Android] BroadcastReceiver

๐Ÿ’ก์•ˆ๋“œ๋กœ์ด๋“œ 4๋Œ€ ์ปดํฌ๋„ŒํŠธ ์ค‘์— ํ•˜๋‚˜์ธ BroadcastReceiver์— ๋Œ€ํ•˜์—ฌ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊ฐœ์š”

  • ์ด์ „์— Broadcast Receiver์™€ BootReceiver์— ๋Œ€ํ•˜์—ฌ ํ•™์Šตํ•˜๊ณ  ๊ทธ์— ๋Œ€ํ•œ ๊ธฐ๋ก์„ ๋‚จ๊ฒผ์Šต๋‹ˆ๋‹ค.

Broadcast Receiver, BootReciver ํ™œ์šฉํ•˜๊ธฐ

 

Broadcast Receiver, BootReciver ํ™œ์šฉํ•˜๊ธฐ

Broadcast Receiver ์•ˆ๋“œ๋กœ์ด๋“œ 4๋Œ€ ์ปดํฌ๋„ŒํŠธ ์ค‘ ํ•˜๋‚˜๋กœ, ๊ฐ์ข… ์•ฑ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์บ์น˜ ํ›„ ๋ฆฌ์‹œ๋ฒ„๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.์ „ํ™”, ๋ฌธ์ž ๋“ฑ์˜ ํ–‰์œ„๊ฐ€ ์™”๋‹ค๋Š” ์•Œ๋ฆผ์„ ๋ฐ›๊ณ 

jinudmjournal.tistory.com

 

  • BroadcastReceiver์— ๋Œ€ํ•˜์—ฌ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ ํ•™์Šตํ•˜๊ณ ์ž ๊ธฐ๋กํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

BroadcastReceiver

  • BroadcastReceiver๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์‹œ์Šคํ…œ ๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฐœ์ƒ์‹œํ‚ค๋Š” BroadcastReceiver Message๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.
  • ์‹œ์Šคํ…œ์ด๋‚˜ ๋‹ค๋ฅธ ์•ฑ์ด ๋ณด๋‚ธ ์ด๋ฒคํŠธ๋ฅผ ์•ฑ์—์„œ ์ˆ˜์‹ ํ•˜๊ณ , ์ด์— ๋Œ€์‘ํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŠน์ง•

  • ์‹œ์Šคํ…œ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ์ˆ˜์‹ 
    • ๋ฐฐํ„ฐ๋ฆฌ ๋ถ€์กฑ, ๋„คํŠธ์›Œํฌ ์ƒํƒœ ๋ณ€๊ฒฝ, SMS ์ˆ˜์‹ , ๋ถ€ํŒ… ์™„๋ฃŒ์™€ ๊ฐ™์€ ์•ˆ๋“œ๋กœ์ด๋“œ ์‹œ์Šคํ…œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ
    • ์•ฑ ๋‚ด๋ถ€์—์„œ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋กœ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ์•ฑ์ด ๋ฐœ์ƒ์‹œํ‚จ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ๋ฅผ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์‹คํ–‰
    • BroadcastReceiver๋Š” UI๊ฐ€ ์—†๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ, ์งง์€ ์‹œ๊ฐ„ ๋™์•ˆ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
    • ๊ธด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๊ฒฝ์šฐ ์„œ๋น„์Šค์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๊ฐœ์š”  |  Background work  |  Android Developers

 

๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๊ฐœ์š”  |  Background work  |  Android Developers

์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๊ฐœ์š” ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜์„ธ์š”. Android ์•ฑ์€ Android ์‹œ์Šคํ…œ์—์„œ ๋ธŒ๋กœ

developer.android.com

์ƒ๋ช…์ฃผ๊ธฐ

  • BroadcastReceiver๋Š” UI๊ฐ€ ์—†๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ, ์ˆ˜์‹ ํ•œ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ์— ๋Œ€ํ•ด ๋น ๋ฅด๊ฒŒ ์‘๋‹ตํ•˜๊ณ  ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
  • onReceive() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์•ฑ์ด ์•„์ง ์‹คํ–‰ ์ค‘์ด์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋„ ์‹œ์Šคํ…œ์—์„œ ์•ฑ์„ ์ž ์‹œ ๊นจ์›Œ์„œ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  • onReceive()์—์„œ ๊ธด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์‹œ์Šคํ…œ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ธด ์ž‘์—…์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ์„œ๋น„์Šค๋กœ ์ž‘์—…์„ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

BroadcastReceiver ๋“ฑ๋กํ•˜๊ธฐ

์ •์  ๋“ฑ๋ก

  • ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๋ฆฌ์‹œ๋ฒ„๋ฅผ ํ™œ์šฉํ•  ๋•Œ Manifest์— ๋“ฑ๋กํ•˜๋Š” ์ •์  ๋“ฑ๋ก ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์‹œ์Šคํ…œ์˜ ๋ถ€ํŒ… ์™„๋ฃŒ ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” BroadcastReceiver๋ฅผ ์ •์˜ํ•˜๊ณ , AndroidManifest.xml์— ๋“ฑ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค.
// ์‹œ์Šคํ…œ ๋ถ€ํŒ… ์™„๋ฃŒ ์ด๋ฒคํŠธ ์ˆ˜์‹ 
class MyBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
            // ๋ถ€ํŒ… ์™„๋ฃŒ ํ›„ ์ˆ˜ํ–‰ํ•  ์ž‘์—…
            Log.d("BroadcastReceiver", "Device booted!")
        }
    }
}
  • receiver๋ฅผ ํ™œ์šฉํ•ด ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application>
    <receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

๋™์  ๋“ฑ๋ก

  • ์ฝ”๋“œ์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ๋“ฑ๋ก ๋ฐ ํ•ด์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
  • Wi-Fi ์ƒํƒœ ๋ณ€๊ฒฝ ์ด๋ฒคํŠธ๋ฅผ ๋™์ ์œผ๋กœ ์ˆ˜์‹ ํ•˜๋ฉฐ, ์•ฑ์ด ์‹คํ–‰ ์ค‘์ผ ๋•Œ๋งŒ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๋ฆฌ์‹œ๋ฒ„๋ฅผ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
class MainActivity : AppCompatActivity() {

    private lateinit var receiver: BroadcastReceiver

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

        receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val action = intent.action
                if (action == WifiManager.WIFI_STATE_CHANGED_ACTION) {
                    // Wi-Fi ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ์ฒ˜๋ฆฌํ•  ์ž‘์—…
                    val wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
                    when (wifiState) {
                        WifiManager.WIFI_STATE_ENABLED -> Log.d("BroadcastReceiver", "Wi-Fi Enabled")
                        WifiManager.WIFI_STATE_DISABLED -> Log.d("BroadcastReceiver", "Wi-Fi Disabled")
                    }
                }
            }
        }

        // ๋™์  ๋ฆฌ์‹œ๋ฒ„ ๋“ฑ๋ก
        val intentFilter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)
        registerReceiver(receiver, intentFilter)
    }

    override fun onDestroy() {
        super.onDestroy()
        // ๋™์  ๋ฆฌ์‹œ๋ฒ„ ํ•ด์ œ
        unregisterReceiver(receiver)
    }
}

์ •๋ฆฌ

  • BroadcastReceiver๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์‹œ์Šคํ…œ ๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ„์˜ ๋น„๋™๊ธฐ ํ†ต์‹ ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ๋กœ ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์ •์  ํ˜น์€ ๋™์  ๋“ฑ๋ก์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ธด ์ž‘์—…์„ ํ”ผํ•˜๊ณ  ํ•„์š”์— ๋”ฐ๋ผ Service์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ 

https://brunch.co.kr/@mystoryg/48