Implicit intent vs Deeplink (Scheme)

2020. 4. 15. 01:09Android

오늘 개발을 하다가 다른 애플리케이션과 통신을 하기 위해 Implicit intent로 구현을 하려던 찰나에 Deeplink가 생각나서 해당 기능으로 구현하면 서 두 기능의 차이점과 또 궁금한 점을 기록하겠습니다.

 

 

Implict intent(묵시/암시적 인텐트)

 암시적 인텐트는 작업을 지정하여 기기에서 해당 작업을 수행할 수 있는 모든 앱을 호출할 수 있도록 합니다. 본인의 앱은 작업을 수행할 수 없지만 다른 앱은 그 작업을 수행할 수 있는 가능성이 있고, 사용자가 어떤 앱을 사용할지 선택하기를 원할 경우에 암시적 인텐트가 유용합니다.

 

인텐트 및 인텐트 필터  |  Android 개발자  |  Android Developers

An Intent is a messaging object you can use to request an action from another app component . Although intents facilitate communication between components in several ways, there are three fundamental use cases: An Activity represents a single screen in…

developer.android.com

Deeplink

 클릭된 링크나 프로그램의 요청이 웹 URI 인텐트를 호출하면 Android 시스템에서는 요청이 성공할 때까지 다음 각 작업을 순서대로 시도합니다.

  1. URI를 처리할 수 있는, 사용자가 선호하는 앱(지정되어 있는 경우)을 엽니다.
  2. URI를 처리할 수 있는, 사용 가능한 유일한 앱을 엽니다.
  3. 사용자가 대화상자에서 앱을 선택하도록 합니다.
 

앱 콘텐츠 딥 링크 만들기  |  Android 개발자  |  Android Developers

사용자가 링크에서 앱에 진입할 수 있도록 하려면 관련 활동의 인텐트 필터를 앱 manifest에 추가해야 합니다. 이러한 인텐트 필터는 모든 활동의 콘텐츠로 연결되는 딥 링크를 허용…

developer.android.com

 

Example

 

  • Implict intent 
// Create the text message with a string
val sendIntent = Intent().apply {   
	action = Intent.ACTION_SEND   
	putExtra(Intent.EXTRA_TEXT, textMessage) 
	type = "text/plain"
} // Verify that the intent will resolve to an activity

if (sendIntent.resolveActivity(packageManager) != null) { 
   	 startActivity(sendIntent)
}

 

 

  • DeepLink(App link)

- 발신하는 곳

startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(uri)))

여기서 uri는 {scheme}://{host_path}형태로 구성이 되며 위에 수신하는 곳을 예로 들면 example://gizmos입니다.

 

또한 uri에 포함하여 parameter를 추가할 수 있습니다. (암시적 인텐트의 putExtra와 비슷한 내용)

{scheme}://{host_path}?parameter1=value1&parameter2=value2

 

위의 방식처럼 uri에 String나 StringBuilder로 추가해서 Uri로 파싱 하면 됩니다.

 

 

- 수신하는 곳

<activity      
	android:name="com.example.android.GizmosActivity"    
    android:label="@string/title_gizmos" >      
    <intent-filter android:label="@string/filter_view_example_gizmos">  
    <action android:name="android.intent.action.VIEW" />    
    
    <category android:name="android.intent.category.DEFAULT" />   
    <category android:name="android.intent.category.BROWSABLE" />  
    <!-- Accepts URIs that begin with "example://gizmos” -->    
    <data 
    	android:scheme="example"              
   		android:host="gizmos" />     
    </intent-filter>    
 </activity>

 

GizmosActivity에서 parameter를 받을 때에는 아래와 같이 사용하면 됩니다.

if (intent.action == Intent.ACTION_VIEW) { // 앱 링크는 해당 인텐트 필터로만 가능하기 때문에
	val value1 = intent.data.getQueryParameter("parameter1");
	val value2 = intent.data.getQueryParameter("parameter2");
}

 

기타 앱이 설치되어 있지 않거나 웹으로 연결하는 방법은 DeepLink를 확인하세요.

 

 

저는 두 가지를 비교하면서 개발하다가 궁금한 점이 생겼습니다.

암시적 인텐트는 다른 애플리케이션을 실행하고 프로세스가 보이지만 딥링크의 경우에는 해당 애플리케이션에서 딥링크가 설정되어있는 화면만 호출됩니다. 프로세스는 애플리케이션의 인스턴스라고 인지하고 있었는데, 이 경우 서로 다른 두 애플리케이션이 하나의 프로세스 안에서 동작하는 것인지, 아니면 프로세스가 다르게 동작하는데 보이지 않던 것인지 궁금합니다. 

 

다음 포스팅에서는 DeepLink로 다른 애플리케이션이 하나의 프로세스 안에서 어떻게 동작하는지 그게 아니라면 어떤 방식인지에 대한 내용을 다루겠습니다.