Search code examples
androidandroid-intentintentfilter

Android: How Intent-Filter works?


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 :)


Solution

  • 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.