Search code examples
androidandroid-manifestintentfilterandroid-implicit-intent

why do we need intent filter in android?


This might be a very stupid question, but I am not quite clear on the answer.

  1. My implicit intent contains an action, data & category (optional), which I pass while sending the intent either through startActivity or startService.

something like this we normally do,

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Standing on the Moon!");
startActivity(intent);

and then we have the same operation done in a different way, using an intent filter in manifest file like

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

My question - is both the way to declare this is for the same purpose, the two different ways they are declared, does they hve different significance???


Solution

  • As I recall, referencing your application as an intent filter from the manifest file will let other applications know that you are capable of handling that intent. Therefore, if you are capable of sending mail and you register yourself this way in your manifest, another application can use yours to send e-mails.

    This is often seen with gallery applications. Applications seldom create their own unless they themselves are a gallery application. Therefore, they will ask the Android system what gallery/mail applications are available, and let you choose one from a list. When you register yourself as an application capable of handling this intent you will find your application in this list.

    I do believe that is one main difference between the two, as the programmatic instantiation is not known by other applications.