Skip to content

Intent & Intent Filters

Intents

We require intent to initiate activity, services, and provide Broadcasts, thus intent is highly crucial. In a nutshell, intent is used to communicate between Android components.

There are two types of intents:

  1. Explicit Intent: It is clearly specified in explicit intent what to do next, which is essentially the name of the component class or package name. Typically, explicit intent is used to start an app component. For example, a well-known component class.

  2. Implicit Intent: Implicit intent does not specify which package or component class to start; instead, it specifies which action to take. For example, the sharing function may offer a variety of options to the user, such as whatsapp, Instagram, or any other media, so the user will have a choice and then the specific action will be performed. For example, dial a phone number, look up a map, or go to a website.

Example of Types of Intent

In the example above, the first action is to click on the Youtube channel name, and the second action is to share the video. Now see in first action app knows where to go, when you click on the channel name to go to the full screen profile of the channel, but In second action there is no mention of sharing because there are many other apps that support video sharing and are recommended to users based on the content type, category, and action to be taken. All of this was accomplished with the aid of an intent filter; let's have a look at what they are.

As previously said, Intent Filters sort apps based on category, action, and content type, which is why they are significant.

Some Important terms

Action: This is essentially what to do; it can be user-defined or system-defined; nonetheless, the majority of the time, two actions are used. ACTION SEND & ACTION VIEW When an app already has data, such as a photo or a URL, that the user can view, VIEW is used. When an app has data and wants to share it with another app, such as social networking or email, SEND is used. There are user-defined actions allowed. One example of a user-defined action is ACTION TIMETRAVEL.

Example:

<action android:name=android.intent.action.VIEW>

<action android:name=android.intent.action.VIEW>

Data: The specification can be just a data type (the mimeType attribute), just a URI, or both a data type and a URI.

Example:

<data android:scheme="string"

    android:host="string"

    android:port="string"

    android:path="string"

    android:pathpattern="string"

    android:mimeType="string" />

Category: The purpose of the category is to specify what kind of application to be sort such as if category is browsable then it will show only the apps which can be able to browse something for example chrome, firefox.

Example:

<category android:name=android.intent.category.BROWSABLE>

Extras: Additional information needed to complete the requested action is carried via key-value pairs. Some actions employ specific types of data URIs, while some actions use specific extras. Extra data is added to the intent using putExtras(), and that data is retrieved using getExtras(). These two arguments are required while doing so. The first is a key element, while the second is a key value.

In adb " --es " we can use to send extra string to intent.

Example of Intent Filters

<activity android:name="MainActivity" android:exported="true">

    <!-- 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" android:exported="false"

      <!-- This activity handles "SEND" action 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"/>

         <action android:name="android.intent.DEFAULT"/>

         <action android:name=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.

Pending Intents

A PendingIntent is a token you send to a other app's (such as NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party apps) that allows the other application to use  application's rights to run a preset piece of code.

If Intent send to a other application, it will execute it with its own permissions. However, if PendingIntent given to a other application, that code will execute Intent with approval.