Intent-filter?

AndroidManifest.xml을 보면 Intent-filter가 존재한다.

Intent-filter는 왜 정의하는 것 일까?

Intent를 전송하는 방식에는 두 가지가 존재한다.

  • 명시적 Intent
  • 암시적 Intent

명시적 Intent는 아래와 같이 Intent를 어디로 전송할 지 정의 한다.

Java
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);

반면 암시적 Intent는 아래와 같이 어디로 전송할 지 정의 하지 않는다.

Java
// Create the text message with a string.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain");

// Try to invoke the intent.
try {
    startActivity(sendIntent);
} catch (ActivityNotFoundException e) {
    // Define what your app should do if no activity can handle the intent.
}

따라서, 암시적 Intent를 수신 받기 위해 우리는 Intent-filter를 사용하게 되는 것이다.

Intent-filter의 요소에는 action, data, category가 존재하며 암시적 Intent 발생 시 3가지 유형을 비교하여 해당 Intent를 수신 받게 된다.

XML
<activity android:name="ShareActivity" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>

예를 들어 위와 같이 설정 되어 있는 경우.

ShareActivity는 데이터 유형이 텍스트이고, ACTION_SEND 인텐트를 수신 받는다.

참고 자료

Android Developers 인텐트 및 인텐트 필터 정보


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *