[Android] 전화 팝업 띄우기

love&peace·2025년 1월 7일

회사에서 후후처럼 전화 수신 시 팝업을 띄우는 프로젝트를 하게되었다.
whowho

문제 발생


compose로 프로젝트를 진행하던 중 오버레이로 (작은 팝업 형태에서) 전화 수신 시 띄우는 것은 성공했는데 아무리해도 시스템 전화 전체화면 위에는 안되고 계속 밑에 그려진다..😿 아무래도 compose를 띄우기 위해 activity를 감싸서 실행한게 문제가 되었다.
그래서 xml 형식으로 띄우기로 결정했다.

전화 감지 Service

@AndroidEntryPoint
class StateService : Service() {

    @Inject
    lateinit var telephonyManager: TelephonyManager
    @Inject
    lateinit var ds: DataStoreUtil
    @Inject
    lateinit var getInfoUsecase: GetInfoUsecase
    
    private val serviceScope = CoroutineScope(Dispatchers.IO + Job())
    
    //..//
    private val phoneStateListener = object : PhoneStateListener() {
        override fun onCallStateChanged(state: Int, phoneNumber: String?) {
            super.onCallStateChanged(state, phoneNumber)
            when (state) {
            	// 전화 수신 감지
                TelephonyManager.CALL_STATE_RINGING -> {
                    showOverlay()
                }

				//전화 종료 감지
                TelephonyManager.CALL_STATE_IDLE -> {
                    removeOverlay()
                }
            }
        }
    }
    
    override fun onCreate() {
        super.onCreate()
		//권한 확인
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED
        ) {
            return
        }
		
        //서비스 실행을 알리는 알림
        createNotificationChannel()

        windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        //전화 감지 리스너
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)
    }
    
    //XML 띄우기
    private fun showOverlay(){ 
    val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        overlayView = inflater.inflate(R.layout.overlay_layout, null)
    //..// 
    }
    private fun createNotificationChannel(){ //..// }

	//종료 시 
	override fun onDestroy() {
        super.onDestroy()
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
        removeOverlay()
        serviceScope.cancel()
    }

0개의 댓글