[안드로이드 프로그래밍] 다른 어플리케이션 실행

PUJIN·2023년 7월 2일
0

android programming

목록 보기
13/26
post-thumbnail

Intent Filter


  • 암시적 intent : 개발자가 허용한 activity만 사용 가능 → 개발자가 activity에 설정한 이름으로 실행 가능
  • 명시적 intent : 사용 불가능

* 같은 어플리케이션 내 intent 사용과 동일



다른 어플리케이션 activity 실행


AndroidManifest.xml

  • 다른 어플리케이션에 접근하려는 경우 : intent filter 이름 설정
  • exported가 false인 경우 : 다른 어플리케이션에서 접근 불가
<activity
	android:name=".MainActivity"
	android:exported="true" >
	<intent-filter>
		<action android:name="com.test.android_startactivity" />

		<category android:name="android.intent.category.DEFAULT" />
	</intent-filter>
</activity>

kotlin file

다른 애플리케이션의 activity에서 지정한 intent filter의 action name 이름으로 Intent 생성

  • startActivity 이용
val newIntent = Intent("com.test.android_startactivity")
newIntent.putExtra("data1", 100)
newIntent.putExtra("data2", "데이터2")
startActivity(newIntent)
  • StartActivityForResult 이용
val c1 = ActivityResultContracts.StartActivityForResult()
val activityLauncher = registerForActivityResult(c1) {
	val value1 = it.data?.getIntExtra("value1",0)
	val value2 = it.data?.getStringExtra("value2")
}
val newIntent = Intent("com.test.android_startactivity")
newIntent.putExtra("data1", 100)
newIntent.putExtra("data2", "데이터2")
activityLauncher.launch(newIntent)



Activity Action


안드로이드에서 제공되는 기본 어플리케이션을 사용할 수 있도록 제공

  • ACTION_VIEW : 실행하는 어플리케이션의 공통적인 이름

지도

  • 위도와 경도 이용
val address = "geo:37.243243,131.861601"
val uri = Uri.parse(address)
val newIntent = Intent(Intent.ACTION_VIEW, uri)
startActivity(newIntent)

웹사이트

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

0개의 댓글