2010년 11월 7일 일요일

자원요소 - Android Manifest.xml

모든 어플리케이션은 루트 디렉토리에 AndroidManifest.xml 파일을 갖고 있어야 한다. 이 파일은 어플리케이션에 대한 기본적인 정보를 안드로이드 시스템에 제공함으로써, 시스템이 어플리케이션 코드를 실행하기 전에 필요한 정보를 갖고 있도록 한다. 이 파일이 수행하는 주요한 일은 다음과 같다:

• 어플리케이션의 자바 패키지 이름을 지정한다. 이 패키지 이름은 어플리케이션의 고유한 식별자로써 작용한다.
• 어플리케이션의 컴포넌트들에 대한 사항을 지정한다-어플리케이션을 구성하는 액티비티, 서비스, 브로드캐스트 리시버, 컨텐트 프로바이더 등을 지정한다. 그리고 각 컴포넌트를 실행시키고 외부에 알리는(예를 들어, 어떤 인텐트 메시지를 처리할 수 있는지) 클래스의 이름을 지정한다. 이렇게 지정함으로써 안드로이드 시스템이 어떤 컴포넌트를 어떤 조건 하에서 실행시킬지 알 수 있도록 한다.
• 어떤 프로세스가 어플리케이션들의 작동을 주관하는지 결정한다.
• 어플리케이션이 다른 API의 보호된 부분에 액세스하고 다른 어플리케이션들과 상호작용하기 위해 가지고 있어야 하는 퍼미션을 선언한다.
• 위와 반대로 다른 어플리케이션이 이 어플리케이션과 상호작용하기 위해 필요한 퍼미션을 정의한다.
• 어플리케이션이 실행될 때 프로필 및 기타 정보를 제공하는 도구 클래스를 선언한다. 이 선언은 어플리케이션이 개발 및 테스트될 때만 존재하며, 어플리케이션이 발매되면 제거된다.
• 어플리케이션에 요구되는 최소 API 레벨을 지정한다.
• 어플리케이션과 연결되어야 하는 라이브러리를 지정한다.

* AndroidManifest 파일의 구조 틀

<?xml version="1.0" encoding="utf-8"?> 

<manifest> 

    <uses-permission /> 
    <permission /> 
    <permission-tree /> 
    <permission-group /> 
    <instrumentation /> 
    <uses-sdk /> 
    <uses-configuration />   
    <uses-feature />   
    <supports-screens />   

    <application> 

        <activity> 
            <intent-filter> 
                <action /> 
                <category /> 
                <data /> 
            </intent-filter> 
            <meta-data /> 
        </activity> 

        <activity-alias> 
            <intent-filter> . . . </intent-filter> 
            <meta-data /> 
        </activity-alias> 

        <service> 
            <intent-filter> . . . </intent-filter> 
            <meta-data/> 
        </service> 

        <receiver> 
            <intent-filter> . . . </intent-filter> 
            <meta-data /> 
        </receiver> 

        <provider> 
            <grant-uri-permission /> 
            <path-permission /> 
            <meta-data /> 
        </provider> 

        <uses-library /> 

    </application> 

</manifest>


ex) NotePad 예의 AndroidManifest.xml 파일

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="com.example.android.notepad"> 
    <application android:icon="@drawable/app_notes" 
                 android:label="@string/app_name" > 

        <provider android:name="NotePadProvider" 
                  android:authorities="com.google.provider.NotePad" /> 

        <activity android:name="NotesList" android:label="@string/title_notes_list"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
            <intent-filter> 
                <action android:name="android.intent.action.VIEW" /> 
                <action android:name="android.intent.action.EDIT" /> 
                <action android:name="android.intent.action.PICK" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 
            </intent-filter> 
            <intent-filter> 
                <action android:name="android.intent.action.GET_CONTENT" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 
            </intent-filter> 
        </activity> 
         
        <activity android:name="NoteEditor" 
                  android:theme="@android:style/Theme.Light" 
                  android:label="@string/title_note" > 
            <intent-filter android:label="@string/resolve_edit"> 
                <action android:name="android.intent.action.VIEW" /> 
                <action android:name="android.intent.action.EDIT" /> 
                <action android:name="com.android.notepad.action.EDIT_NOTE" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 
            </intent-filter> 
            <intent-filter> 
                <action android:name="android.intent.action.INSERT" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 
            </intent-filter> 
        </activity> 
         
        <activity android:name="TitleEditor"  
                  android:label="@string/title_edit_title" 
                  android:theme="@android:style/Theme.Dialog"> 
            <intent-filter android:label="@string/resolve_title"> 
                <action android:name="com.android.notepad.action.EDIT_TITLE" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
                <category android:name="android.intent.category.ALTERNATIVE" /> 
                <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> 
                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 
            </intent-filter> 
        </activity> 
         
    </application> 
</manifest>


Intent의 개념

안드로이드 애플이케이션의 세 개 핵심 컴포넌트-액티비티,서비스,브로드캐스트 리시버-는 인텐트라는 메시지를 통해 실행된다. 인텐트 메시징은 같거나 다른 애플리케이션에 있는 컴포넌트들간의 런타임 바인딩을 제공하는 장치이다.
Intent 클래스의 객체인 intent 자체는 수행할 오퍼레이션의 추상적인 설명을 담고 있는 수동적인 데이터 구조이다.

HTTP의 동사(GET,POST 등)와 자원(URL)의 구조처럼, 안드로이드의 인텐트도 Action + Context 의 형태를 갖는다. 안드로이드가 훨씬 더 많은 action과 context component를 갖고 있지만 그 개념을 똑같다.
웹 브라우저가 동사+URL 구조를 어떻게 처리해야 하는지 아는 것처럼, 안드로이드도 어떻게 주어진 인텐트를 처리할 액티비티를 찾거나  다른 프로그램의 로직을 찾는지 알고 있다.

각 컴포넌트에 대해 인텐트를 전달하는 메카니즘은 다음과 같다:
•액티비티를 새로 시작하거나 기존 액티비티가 새로운 일을 하도록 할 경우, Context.startActivity() 나 Activity.startActivityForResult() 에 인텐트 객체를 전달한다.
•서비스를 시작하거나 실행중인 서비스에 새로운 명령을 전달할 때, Context.startService()에 인텐트 객체를 전달한다. 유사한 형태로, 호출하는 컴포넌트와 대상 서비스간에 연결을 만들 경우, Context.bindService()에 인텐트 객체를 전달한다. 만약 서비스가 실행 중이 아닐 경우 그 서비스를 시작시킬 수도 있다.
•브로드캐스트 메쏘드(Context.sendBroadcast(), Context.sendOrderedBroadcast(), Context.sendStickyBroadcast() 등과 같은)에 전달된 인텐트 객체는 모든 관심 있는 브로드캐스트 리시버에게 전달된다. 대부분의 브로드캐스트는 시스템 코드에서 시작된다.

대상컴포넌트가 지정되어 있는 Explicit intent와 지정된 대상 컴포넌트가 없는 Implicit intent로 구분할 수 있다.

Intent Filter

대상 컴포넌트를 지정하지 않은 인텐트의 경우, 대상 컴포넌트가 될 가능성이 있는 컴포넌트(잠재 컴포넌트)에 대해 실행 대상이 되는지 테스트해야 한다.

이 테스트의 기준으로 정해 놓은 것이 intent filter이다. 필터에는 그 컴포넌트가 수행하는 일이나 처리할 수 있는 인텐트 등이 명시된다. 대상 컴포넌트가 되는지를 테스트할 때는 인텐트 필터의 다음 세 가지 항목을 점검한다:
action, data (both URI and data type), category

extras 나 flags는 아무 역할도 하지 않는다.

인텐트 필터는 IntentFilter 클래스의 인스턴스이다. 하지만, 그 컴포넌트를 실행하기 전에 안드로이드 시스템이 그 능력에 대해 알고 있어야 하기 때문에, 인텐트 필터는 보통 Java 코드에 설정되지 않고 애플리케이션의 매니페이스 파일(AndroidManifest.xml)에 <intent-filter> 요소로 정의된다. (한 가지 예외는 브로드캐스트 리시버인데 이것은 Context.registerReceiver()를 호출함으로써 동적으로 등록되며, 그러면 바로 IntentFilter 객체로 생성된다.)
필터는 인텐트 객체의 action, data, category 필드에 해당하는 필드를 갖고 있다. implicit intent는 이 세 영역에 대해 테스트된다. 필터를 정의한 컴포넌트가 인텐트를 갖고 있는 컴포넌트에게 전달되려면 이 세 가지 테스트를 통과해야 한다.

댓글 없음:

댓글 쓰기