Archives
Recent Posts
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Today
Total
관리 메뉴

안드로이드 개발자의 창고

[37일차 Android] 다른 앱의 Activity 실행하기 본문

Computer/Android

[37일차 Android] 다른 앱의 Activity 실행하기

Wise-99 2023. 6. 23. 12:12

 

 

 

출처 : 안드로이드 앱스쿨 2기 윤재성 강사님 수업 PPT

 

 

 

📖 Intent Filter

  • 안드로이드의 4대 구성요소는 모두 AndroidManifest.xml 파일에 기록되어야 한다.
  • 이 때 다른 애플리케이션이 실행할 수 있도록 하고자 한다면 Intent filter 를 이용해 이름을 설정해주면 된다.
  • 애플리케이션이 단말기에 설치되면 안드로이드 OS는 지정된 Intent Filter 의 이름을 확인하여 정리하고 실행 요청을 받으면 이를 실행할 수 있다.

 

 

 

예제 코드

AndroidManifest.xml

        <activity
            android:name=".ThirdActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="com.test.adnroid46_third_activity"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true"></activity>
  • ThirdActivity 
    • <action android:name="com.test.adnroid46_third_activity"/>로 다른 앱에서 실행할 수 있도록 이름을 지정한다.
    • android:exported="true"를 false로 설정하면 외부 앱에서 해당 Activity를 실행할 수 없다.
  • SecondActivity
    • <action android:name="android.intent.action.MAIN" /> 로 시작 Activity를 SecondActivity로 지정하였다.
    • 해당 앱을 실행하면 SecondActivity가 처음으로 실행된다.

 

 

 

ThridActivity.kt

class ThirdActivity : AppCompatActivity() {

    lateinit var activityThirdBinding: ActivityThirdBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activityThirdBinding = ActivityThirdBinding.inflate(layoutInflater)
        setContentView(activityThirdBinding.root)

        activityThirdBinding.run {
            val data1 = intent.getIntExtra("data1", 0)
            val data2 = intent.getStringExtra("data2")

            textViewResult1.text = "data1 : ${data1}\n"
            textViewResult1.append("Data2 : $data2")

            button.run {
                setOnClickListener {
                    val resultIntent = Intent()
                    resultIntent.putExtra("value1", 200)
                    resultIntent.putExtra("value2", "감사합니다")

                    setResult(RESULT_OK, resultIntent)
                    finish()
                }
            }
        }
    }
}
  • 외부 앱에서 ThirdActivity를 실행할 때 데이터를 주고 받는 코드
    • data1과 data2를 받아 ThirdActivity에서 보여주고 value1과 value2를 intent에 담아 외부 앱의 Activity로 넘겨준다.

 

 

 

외부 앱의 MainActivity.kt

class MainActivity : AppCompatActivity() {

    lateinit var activityMainBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(activityMainBinding.root)

        val c1 = ActivityResultContracts.StartActivityForResult()
        val activityLauncher = registerForActivityResult(c1){
            val value1 = it.data?.getIntExtra("value1", 0)
            val value2 = it.data?.getStringExtra("value2")

            activityMainBinding.run{
                textView.run{
                    text = "value1 : ${value1}\n"
                    append("value2 : ${value2}")
                }
            }
        }

        activityMainBinding.run {
            button.run {
                setOnClickListener {

                    val newIntent = Intent("com.test.adnroid46_third_activity")

                    newIntent.putExtra("data1", 100)
                    newIntent.putExtra("data2", "안녕하세요")

                    // startActivity(newIntent)
                    activityLauncher.launch(newIntent)
                }
            }

            buttonShowMap.run{
                setOnClickListener {
                    // 위도와 경도를 문자열로 만들어준다.
                    // val address = "geo:37.243243,131.861601"
                    // val uri = Uri.parse(address)
                    // val newIntent = Intent(Intent.ACTION_VIEW, uri)

                    // 웹사이트
                    val address = "http://developer.android.com"
                    val uri = Uri.parse(address)
                    val newIntent = Intent(Intent.ACTION_VIEW, uri)

                    startActivity(newIntent)
                }
            }
        }
    }
}
  • button
    • val newIntent = Intent("com.test.adnroid46_third_activity")
      • 다른 애플리케이션의 Activity에 붙여준 이름을 지정하여 Intent를 생성한다.
      • Intent Filter의 Action name을 넣어준다.
  • buttonShowMap
    • // val address = "geo:37.243243,131.861601"
      • 위도와 경도를 문자열로 만든다.
      • 그 문자열을 Uri로 변환하여 Intent로 전달한다.
      • 이로 인해 보여지는 결과는 지도 화면으로 연결된다.
    • val address = "http://developer.android.com"
      • 해당 문자열을 Uri로 변환하여 Intent로 전달한다.
      • 이로 인해 보여지는 결과는 안드로이드 공식 홈페이지이다.

 

 

 

결과

외부 앱 연결(button)

 

 

 

지도 연결(button2 주석 처리된 코드)

 

 

 

외부 사이트 연결(button2)

'Computer > Android' 카테고리의 다른 글

[37일차 Android] SnackBar  (0) 2023.06.23
[37일차 Android] Toast  (0) 2023.06.23
[34일차 Android] OnActivityResult  (0) 2023.06.23
[34일차 Android] Activity 실행하기  (0) 2023.06.17
[34일차 Android] Activity  (0) 2023.06.17