1

I read another post: Android: Understanding Intent-Filters but I still can't grasp what really Intent-filters do and how they work.

For example:

What is the difference between:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>

And

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>

Why in the first case the app icon is showed in the app list and in the second case it is not?

When I need to open an Intent-filter and close it?

If I do:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />
         </intent-filter>
         <intent-filter>
            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>

Is it correct?

Thanks in advance for the replies and clarifications :)

Community
  • 1
  • 1
Dango
  • 311
  • 1
  • 4
  • 7

3 Answers3

1

Intent filters are supposed to be added BETWEEN the opening and closing tags of a receiver, service or activity. They signify the "implicit intents" that the app can handle. In your app menu, where you have all your apps listed, android looks for the intent Main and Launcher. Whichever apps have that as an intent filter, those get displayed, and the activity associated with Main, Launcher gets called as soon as the user opens the app.

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

These two intent filters associated with my activity called MainActivity tell android to 1) Place my app in the menu. 2) Open MainActivity as soon as the user selects the app. Hence you should have only one activty with Main and Launcher as its intent filters.

For example if user selects share button and its an implicit intent, then the apps that have "share option" in the form of an filter can be called via a dialog box/ selector.

EDIT :

<

activity android:name="ShareActivity">
    <!-- This activity handles "SEND" actions with text data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
    <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
    </intent-filter>
</activity>

The first activity, MainActivity, is the app's main entry point—the activity that opens when the user initially launches the app with the launcher icon:

    The ACTION_MAIN action indicates this is the main entry point and does not expect any intent data.
    The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in the system's app launcher. If the <activity> element does not specify an icon with icon, then the system uses the icon from the <application> element.

These two must be paired together in order for the activity to appear in the app launcher.

The second activity, ShareActivity, is intended to facilitate sharing text and media content. Although users might enter this activity by navigating to it from MainActivity, they can also enter ShareActivity directly from another app that issues an implicit intent matching one of the two intent filters.

http://developer.android.com/guide/components/intents-filters.html Take a look at this site. So intent filters describe what the activity CAN do, how it CAN be started (via another app or the main launcher or a browser) and what additional functions it can do.

Varun Agarwal
  • 1,587
  • 14
  • 29
  • "Intent filters are supposed to be added BETWEEN the opening and closing tags of a receiver, service or activity" ok, I have understood at least that much, but I wrote an example where the result is drastically different one from the other even if the intents are between the same activity. Why? From what you wrote it seems that if you do: Activity intent-filter /Activity is the same as Activity intent-filter intent-filter /Activity. I am stil confused sorry :D – Dango Oct 13 '15 at 12:04
  • have a look at the edit and the link. If your activity can be accessed in different ways, then its better to separate the actions and keep them in different intent filters but within the same activity tag. Hence the first case seems like the best solution for you. – Varun Agarwal Oct 13 '15 at 12:51
  • 1
    Ok, so is it better to divide datas, categories and actions in different intent-filters because the OS interpret it differently depending on how you wrote it? Or there is a logic behind it? Sorry, I am dumb and I need things to be explained thoroughly :/ – Dango Oct 13 '15 at 13:05
  • its better to divide actions so the OS knows how to handle the activity and when to call it. For each different action you mention the data or category thats associated with that. Main and Launcher have nothing special in them. But if your activity can be started by 'share' then you have to mention that the activity can send sms, receive etc. – Varun Agarwal Oct 13 '15 at 13:13
  • Ok, now I understand :D Thank you for your patience :) – Dango Oct 13 '15 at 13:37
  • No problem. Do accept my answer if your doubts have been clarified :) – Varun Agarwal Oct 13 '15 at 13:38
  • If nobody else answer with a more detailed answer, I will accept it :) – Dango Oct 13 '15 at 15:39
0

Accordingly to this guide:

To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an element in your manifest file. Each intent filter specifies the type of intents it accepts based on the intent's action, data, and category. The system will deliver an implicit intent to your app component only if the intent can pass through one of your intent filters.

An app component should declare separate filters for each unique job it can do.

Each intent filter is defined by an element in the app's manifest file, nested in the corresponding app component (such as an element). Inside the , you can specify the type of intents to accept using one or more of these three elements: action , data, category

It's okay to create a filter that includes more than one instance of action, data, or category. If you do, you simply need to be certain that the component can handle any and all combinations of those filter elements.

Example filters

To better understand some of the intent filter behaviors, look at the following snippet from the manifest file of a social-sharing app.

<activity android:name="MainActivity">
    <!-- This activity is the main entry, should appear in app launcher -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="ShareActivity">
    <!-- This activity handles "SEND" actions with text data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
    <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
    </intent-filter>
</activity>

For more information read the article.

Community
  • 1
  • 1
Anton Kovalyov
  • 932
  • 4
  • 13
  • Thanks for the reply, it's really similar to the android developers post about intents. But I can't understand if action, category, data why sometimes can be all together and other times they need to be divided – Dango Oct 13 '15 at 11:57
  • each intent filter must have its own action, category and data combination that is needed. – Anton Kovalyov Oct 13 '15 at 12:20
0

Read following links. These will give you a good idea about intent filters

http://www.tutorialspoint.com/android/android_intents_filters.htm

http://codetheory.in/android-intent-filters/

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32