๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿค2024 ์•ˆ๋“œ๋กœ์ด๋“œ/โ˜€๏ธ ํ•˜๊ณ„ ์ธํ„ด ๊ฐœ๋ฐœ ํ”„๋กœ์ ํŠธ ๊ธฐ๋ก

Notification์— ์œ„์น˜ ์—…๋ฐ์ดํŠธ ๋ฐฉ์‹ ๋ณ€๊ฒฝ

by hyeonha 2024. 11. 1.

๋ชฉ์ฐจ

    ๋กœ์ง์„ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋œ ๋ฐฐ๊ฒฝ

    ๊ธฐ์กด ์œ„์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ์•Œ๋ฆผ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๋กœ์ง์„ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค. 

    ์•„๋ž˜ ๋‚ด์šฉ๊ณผ ๊ฐ™์ด ๊ธฐ์กด์—๋Š” ์ธก์ •๋œ ์œ„์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์•Œ๋ฆผ์— ์—…๋ฐ์ดํŠธํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด์—ˆ๋‹ค.

     

    ๊ทธ๋Ÿฌ๋‚˜ ๊ฑด์„ค ํ™˜๊ฒฝ์˜ ๊ทผ๋กœ์ž๋ถ„๋“ค์ด ๋งค๋ฒˆ ์›Œ์น˜ ๋‚ด์—์„œ ์œ„์น˜๋ฅผ ํ™•์ธํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์ ๊ณ  ์ด๋Š” ์„œ๋ฒ„๋กœ ์ „์†ก๋˜์–ด ๊ด€๋ฆฌ์ž๋ถ„๋“ค์ด ํ™•์ธํ•ด์ฃผ๋Š” ํ”„๋กœ์„ธ์Šค์—์„œ๋Š” ๊ธฐ์กด ๋ฐฉ์‹์ด ๋ถˆํ•„์š”ํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜์˜€๋‹ค.  ๋˜ํ•œ ๋ฐฐํ„ฐ๋ฆฌ ์†Œ๋ชจ์œจ๋„ ๋งŽ์•˜๋˜ ๋ฐฉ์‹์ด๋ผ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค!

     

    ๋”ฐ๋ผ์„œ ์•ฑ์ด ํ™”๋ฉด์— ๋ณด์ผ ๋•Œ๋งŒ ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ์ฃผ์†Œ๊ฐ’์„ ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

     

    ์„œ๋น„์Šค์—์„œ ์•กํ‹ฐ๋น„ํ‹ฐ๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก

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

    ์ด๋ฅผ ์œ„ํ•ด "๋ฐ”์ธ๋“œ ๋œ ์„œ๋น„์Šค" ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

    class LocationForegroundService : Service() {
    
    // ๊ฐ€์žฅ ์ตœ๊ทผ ์œ„์น˜ ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
        private var lastLocation : Location? = null
    
    // ์„œ๋น„์Šค์—์„œ ์•กํ‹ฐ๋น„ํ‹ฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด๋ถ€ ํด๋ž˜์Šค ์ƒ์„ฑ
    
    // LocationBinder๋Š” LocationForegroundService๊ฐ์ฒด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ getService ๋ฉ”์„œ๋“œ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ œ๊ณตํ•ด์ค€๋‹ค. 
    // ์ด๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋น„์Šค์˜ getLastLocation() ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค
          inner class LocationBinder :Binder() {
            fun getService() : LocationForegroundService  {
            // ํ˜„์žฌ ์„œ๋น„์Šค ๊ฐ์ฒด ๋ฐ˜ํ™˜
                return this@LocationForegroundService
            }
        }

     

    ์ด ๋•Œ ๊ทธ๋ƒฅ return LocationForegroundSerivce() ๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฆฌํ„ด์‹œ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์ด ๋˜์ง€ ์•Š๊ณ  null๋กœ ๋–ด์—ˆ๋‹ค.

    return์—์„œ๋Š” ํ˜„์žฌ ์„œ๋น„์Šค ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์ค˜์•ผํ•œ๋‹ค!

     

    ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋„๋ก ๊ณต๊ฐœ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.  lastLocation์—๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋‹ค.

       fun getLastLocation() : Location? {
            return lastLocation
        }

     

    ๊ทธ๋ฆฌ๊ณ  ์„œ๋น„์Šค์˜ onBind ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ์—์„œ Binder ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

        override fun onBind(intent: Intent?): IBinder? {
            return LocationBinder()
        }

     

      // bindService ๋ฉ”์„œ๋“œ์— ๋„˜๊ฒจ์ค„ ์„œ๋น„์Šค ๋ฐ”์ธ๋”ฉ ์ฝœ๋ฐฑ์„ ์ •์˜ํ•ด์ค€๋‹ค.
      
      private val connection = object  : ServiceConnection{
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
               val binder = service as LocationForegroundService.LocationBinder
                locationService = binder.getService()
                bound = true
            }
    
            override fun onServiceDisconnected(name: ComponentName?) {
                bound = false
            }	
           }

     

    ์„œ๋น„์Šค์— ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

       private fun startLocationService() {
            Intent(applicationContext, LocationForegroundService::class.java).also { intent ->
                startForegroundService(intent)
                bindService(intent,connection,Context.BIND_AUTO_CREATE)
            }
        }

     

    ๊ทธ๋ฆฌ๊ณ  ์•ž์„œ ์„œ๋น„์Šค์—์„œ ์ •์˜ํ•ด์ค€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์œ„๋„, ๊ฒฝ๋„ ๊ฐ’์„ ์–ป๊ณ  ์ด๋ฅผ ํ†ตํ•ด ์ฃผ์†Œ๋ฅผ ๊ฐ€๊ณตํ•ด์ฃผ์—ˆ๋‹ค.

    // ์ฃผ์†Œ ๊ฐ€๊ณตํ•ด์ฃผ๊ธฐ 
    private fun updateLocationDisplay() {
            if (bound) {
                lastLocation = locationService.getLastLocation()

     

    ์ด ๋•Œ ์ฃผ์†Œ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์€ ํ™”๋ฉด์— ์•ฑ์ด ๋ณด์ผ ๋•Œ์—ฌ์•ผํ•œ๋‹ค.

    ๋”ฐ๋ผ์„œ onStart ๋‚ด๋ถ€์—์„œ ์•ฑ์ด ํ™”๋ฉด์— ๋ณด์ด๊ณ , ์„œ๋น„์Šค์™€ ์—ฐ๊ฒฐ๋œ ๊ฒฝ์šฐ์—๋งŒ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ๋˜๋„๋ก ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ๋‹ค.

     override fun onStart() {
            super.onStart()
            
            if(bound) {
                updateLocationDisplay()
            }
        }
        
      // ๋‹ค์‹œ 
        override fun onResume() {
            super.onResume()
            Intent(this,LocationForegroundService::class.java).also {intent->
                startForegroundService(intent)
                bindService(intent,connection, Context.BIND_AUTO_CREATE)
            }
            if(bound) {
                updateLocationDisplay()
            }
        }
    
    // ๋งŒ์•ฝ ํ™”๋ฉด์— ๋ณด์ด์ง€ ์•Š์œผ๋ฉด ์—ฐ๊ฒฐ์„ ํ•ด์ œํ•œ๋‹ค.
        override fun onStop() {
            super.onStop()
            if (bound) {
                unbindService(connection)
                bound = false
            }
        }

     

    ๊ฒฐ๊ณผ ์˜์ƒ!! 

    ์œ„์น˜(์œ„๋„ ๊ฒฝ๋„) ์ธก์ • ํ›„ ์ฃผ์†Œ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๊ธฐ

     

    728x90