마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Foreground서비스에서 Activity로 브로드캐스트 보내기

0 추천

안녕하세요 현재 공부용으로 만드는 앱에서 사용자 위치가 변경될 때마다

액티비티로 좌표를 보내서 마커가 이동하도록 하는 기능을 구현하려고 하는데요.

Foreground서비스로 실시간으로 좌표 받는 부분까진 완료했는데

정작 지도 Activity로 좌표를 보내는 부분이 안되서 막혀있네요.

인터넷에서 검색한 대로 했는데 왜 안될까요..

private fun cngMapLocation(latitude: Double, longitude: Double){
        var intent = Intent(this, MapActivity::class.java)
        intent.putExtra("latitude", latitude)
        intent.putExtra("longitude", longitude)
        intent.action = App.CNG_LOC
        Log.e("브로드 캐스트", "전송 ${intent.action}")
        sendBroadcast(intent)
    }

Foreground서비스의 보내는 쪽 코드

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMapBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        mapView = findViewById(R.id.map_view)
        mapView.onCreate(savedInstanceState)
        mapView.getMapAsync(this)

        val br: BroadcastReceiver = locReceiver()
        val filter = IntentFilter().apply {
            addAction(App.CNG_LOC)
        }
        registerReceiver(br, filter)
    }
    inner class locReceiver: BroadcastReceiver(){                                           //브로드 캐스트 리시버
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.d("브로드 캐스트", "받음")
            var latitude = intent?.getDoubleExtra("latitude", 0.0)
            var longitude = intent?.getDoubleExtra("longitude", 0.0)
            if(latitude != null && longitude != null){
                Log.d("브로드 캐스트", "위치 변경")
                changePosition(latitude, longitude)
            }
        }
    }

이게 지도 Activity쪽 브로드캐스트 받는 코드입니다.

혹시 보이는 문제점 있으면 알려주시면 감사하겠습니다!

따로 오류는 발생하지 않았고, 언어는 Kotlin입니다.

val filter = IntentFilter(App.CNG_LOC)

이렇게 하는 방법도 사용해봤는데 안되더라구요

bonon (620 포인트) 님이 2021년 2월 27일 질문

1개의 답변

0 추천
spark (226,420 포인트) 님이 2021년 2월 28일 답변
...