Development/Android

외부 웹 사이트에서 내 앱으로 공유 기능 활성화하기 - Intent Sharing

SeungYong.Lee 2025. 5. 22. 21:13
반응형

- 내가 편하게 쓰려고 메모장 앱을 간단하게 만들고 있는데, 외부 웹 사이트에서 링크를 쉽게 공유하고 싶어 내 앱으로의 Intent 공유 기능을 활성화했다.

 

- 과정은 다음과 같다. 일단 Intent로 공유받는 데이터를 수신할 Activity를 하나 생성해 준다.

class ReceiveShareActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val sharedUrl = if (intent?.action == Intent.ACTION_SEND && intent.type == "text/plain") {
            intent.getStringExtra(Intent.EXTRA_TEXT)
        } else null
        setContent {
            MindBankTheme {
                if (sharedUrl != null) {
                    Text("받은 URL: $sharedUrl")
                } else {
                    Text("공유된 URL이 없습니다.")
                }
            }
        }
    }
}

 

- 그다음, AndroidManifest에 아래와 같이 생성한 Activity를 등록해 준다.

<activity
    android:name=".presentation.navigation.activity.ReceiveShareActivity"
    android:exported="true"
    android:label="@string/title_activity_receive_share"
    android:theme="@style/Theme.MindBank" >
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

- Text Type 데이터를 앱으로 공유 가능하다.

 

- 앱 외부에서 호출 가능한지 지정해 주는 exported는 반드시 true로 설정

반응형