앞에서 설명했듯이 Application의 core component-activity, service and broadcast receiver-는 Intent라고 불리는 메시지를 통해 활성화 된다.
Intent messaging은 동일한 또는 다른 Application의 component 간의 최근 run-time binding에 대한 기능(facility)이다.
intent 그 자체는 - Intent object- 수행할 업무의 추상정인 description을 가지고 있는 passive data structure 이다.
Application Core Component 별로 Intent는 아래와 같은 경우에 사용된다.
1. Intent Object는 activity를 실행하거나 새로운 일을 하고 있는 activity를 얻기위해 Context.startActivity()나 Activity.startActivityForResult()에 전달된다.
2. Intent는 Service를 초기화하거나 진행중인 service에 새로운 지시를 전달하기 위해 Context.startService()에 전달된다.
3. broadcast method로 전달되는 Intent는 모든 관계된 broadcast receiver에 전달된다.
위의 각각의 경우에 대해 안드로이드는 intent에 상응하는 적당한 activity, service 또는 broadcast receiver를 찾고, 필요하다면 그것들을 instance로 만든다.
이러한 messaging system에 대해서는 중복이 없다. 즉 startActivity()로 전달된 Intent는 Activity에만 전달이 되며, service나 broadcast receiver에 전달되지 않는다는 의미이다.
Intent Objects
Intent Object는 정보의 묶음이다.
Intent는 다음과 같은 정보를 포함한다.
1. Component name
Intent를 다루는 Component name으로 ComponentName Object(fully qualified class name like com.example.HelloWorld)과 application manifest file에 설정된 package name 이다. - Component name의 package와 mainfest에 설정된 package는 일치하지 않아도 된다.
Component name은 optional이며 설정되어 있으면 intent object는 명시된 class의 instance에 전달되고, 설정되어있지 않으면 안드로이드는 Intent의 다른 정보를 적절한 target을 정하기 위해 사용한다.
Compoennt name은 setComponent(), setClass()나 setClassName() method를 통해 설정하며, getComponent() method를 통해 조회된다.
2. Action
수행될 Action의 string naming.
Intent class는 아래와 같이 다양한 action constants를 정의한다.
Constant | Target component | Action |
ACTION_CALL | activity | Initiate a phone call |
ACTION_EDIT | activity | Display data for the user to edit |
ACTION_MAIN | activity | Start up as the initial activity of a task, with no data input and no returned output |
ACTION_SYNC | activity | Synchronize data on a server with data on the mobile device |
ACTION_BATTERY_LOW | broadcast receiver | A warning that the battery is low |
ACTION_HEADSET_PLUG | broadcast receiver | A headset has been plugged into the device, or unplugged from it |
ACTION_SCREEN_ON | broadcast receiver | The screen has been turned on |
ACTION_TIMEZONE_CHANGED | broadcast receiver | The setting for the time zone has changed |
Intent의 Action은 setAction() method를 통해 설정하며, getAction() method를 통해 조회된다.
3. Data
Intent의 Data는 수행할 data의 URI와 그 data의 MIME type이다. 서로 다른 Action은 서로 다른 종류의 Data와 쌍으로 이루어져 있다. 예를 들어 Action이 ACTION_EDIT라면 Data는 수정될 문서의 URL를 포함하며, ACTION_CALL일라면 "tel:전화번호"로 이루어 진다.
Intent를 Component와 매칭시키는 경우 그 Data의 Type을 인지하는 것이 중요하다. 예를 들면 audio type은 audio를 출력하는 Component로 매칭을 시켜야 하기 때문이다.
대부분의 경우 Data type은 URI를 통해 추정되는데, 예를 들어 "content:" URI는 Data가 단말에 위치하고 ContentProvider에 의해 제어된다는 것을 알 수 있다. 하지만 Data type은 Intent Object에서 명확히 설정될 수 있다.
Intent의 Data는 setData() method를 통해, MIME type은 setType() method를 통해 설정할 수 있으며, setDataAndType() method를 통해 한 번에 설정될 수 있으며, getData(), getType() method를 통해 조회할 수 있다.
4. Category
Category는 Intent를 다루는 Component의 종류에 대해 부가적인 정보를 포함하는 string이다.
Intent Object에는 몇 개의 category 정보가 있을 수 있는데, Action과 마찬가지로 Intent class는 몇 개의 category constants를 가지고 있다.
Constant | Description |
CATEGORY_BROWSABLE | The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message. |
CATEGORY_GADGET | The activity can be embedded inside of another activity that hosts gadgets |
CATEGORY_HOME | The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed. |
CATEGORY_LAUNCHER | The activity can be the initial activity of a task and is listed in the top-level application launcher |
CATEGORY_PREFERENCE | The target activity is a preference panel |
Intent Object에 Category를 추가하기 위해 addCategory(), 이전에 추가된 Category를 삭제하기 위해 removeCategory() mehotd를 사용하며, 현재 Object의 모든 Category를 조회하기 위해 getCategories() method를 사용한다.
5. Extras
Intent를 다룰 Component에 전달되는 부가적인 정보의 key-value.
몇몇 Action들이 특정한 종류의 data URI와 쌍으로 구성되듯이, 몇 몇은 특정 Extras와 쌍으로 구성된다. 예를 들어 ACTION_TIMEZONE_CHANGED intent는 새로운 Timezone을 나타내는 "time-zone" extra를 가지고 있다. 또한 ACTION_HEADSET_PLUG Action 은 현재 headset이 plug되어 있는지를 나타내는 "state" extra를 가지고 있다. 만일 사용자가 정의하는 SHOW_COLOR라는 Action이 있다면 color value는 extra key-value를 설정하게 될 것이다.
Extras는 putExtras()와 getExtras() method 를 사용하여 extras를 설정하고 조회할 수 있다.
6. Flags
Intent class에 정의되어 있는 Flag를 참조
Intent Resolution
Intent는 두가지의 Group으로 구분할 수 있다.
1. Explicit Intent
Component name으로 target component를 명시한다. 일반적으로 다른 Applicaiton의 개발자에세 Component name이 공개되지 않으므로, explicit intents는 전형적인 application internal message이다.
2. Implicit Intent
Target component를 명시하지 않으며, 다른 Application의 component를 활성화하기위해 사용된다.
안드로이드는 명시된 target class의 instance에 explicit intent를 전달한다. Component name이 중요하다.
target 이 명시되지 않는 경우 안드로이드는 intent를 다룰 가장 적합한 Component를 찾아야 한다. 이 경우는 Intent Object를 Intent filter와 비교함으로써 진행된다. intent filter는 잠재적으로 intent를 수신할 수 있는 Component와 관련된 structure이다. Filter는 component의 특성을 공시하고 그것이 드룰 수 있는 intent를 정한다.
Component가 filter를 가지지 않으면 이는 explicit intent만을 수용하겠다는 의미이다.
Action, data, category만이 intetn filter에서 적용할 수 있고, extras나 flag는 resolving의 대상이 되지 않는다.
Intent filters
어떤 implicit intent를 다룰수 있는지 시스템에 알리기 위해 Activity, Service 및 Broadcast Receiver는 하나 또는 그 이상의 intent filter를 가진다. 각 filter에는 component의 특성, 수신하려는 intent의 종류를 기술한다.
explicit intent는 어떤 것이 포함되어 있던간에 target Component에 전달된다. 하지만 implicit intent는 filter에 정의된 component로만 전달된다.
Intent Filter는 IntentFilter class의 instance이다. 하지만 안드로이드 System은 component를 launch하기 전에 해당 Component에 대한 특성을 알아야 하기에 Source code가 아니라 manifest file에 intent filter를 설정한다. (단 broascast receiver의 경우는 동적으로 등록되기 때문에 제외된다.)