사용가능한 앱이 여러개인 경우 Intent Chooser

뭉삐·2024년 5월 10일

위 이미지처럼 특정 기능을 사용하는 앱을 실행할때 원하는 앱을 선택해본 경험이 있을겁니다.

지도, 메일보내기, 웹브라우저 실행등 예시들이 더 있겠네요.

구현방법은 간단합니다.

protected fun moveToStore() {
        // Google Play Store로 이동하는 Intent 생성
        val playStoreIntent = Intent(Intent.ACTION_VIEW).apply {
            data = Uri.parse("http://play.google.com/store/apps/details?id=${BuildConfig.APPLICATION_ID}")
        }

        // One Store로 이동하는 Intent 생성
        val oneStoreIntent = Intent(Intent.ACTION_VIEW).apply {
            data = Uri.parse("onestore://common/product/${BuildConfig.ONE_STORE_PID}")
        }

        // Intent 리스트 생성
        val intents = ArrayList<Intent>().apply {
            add(playStoreIntent)
            add(oneStoreIntent)
        }

        // Intent Chooser 생성
        val chooserIntent = Intent.createChooser(Intent(), "Select Store").apply {
            putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toTypedArray())
        }

        // Chooser 실행
        startActivity(chooserIntent)
    }

간단하죠?

사용하고 싶은 Intent들을 ArrayList로 만들어서 createChooser함수에게 전달하면 됩니다.

profile
kotlin 주력 앱개발자

0개의 댓글