In android, Intent Filter is an expression in the app’s manifest file (ActivityMainfest.xml) and it is used to specify the type of intents that the component would like to receive. In case if we create Intent Filter for an activity, there is a possibility for other apps to start our activity by sending a certain type of intent otherwise the activity can be started only by an explicit intent.
Generally, the Intent Filters (<intent-filter>) whatever we define in the manifest file can be nested in the corresponding app components and we can specify the type of intents to accept using these three elements.
<action>
It defines the name of an intended action to be accepted and it must be a literal string value of an action, not the class constant.
<category>
It defines the name of an intent category to be accepted and it must be the literal string value of an action, not the class constant.
<data>
It defines the type of data to be accepted and by using one or more attributes we can specify various aspects of the data URI (scheme, host, port, path) and MIME type.
Following is the code snippet of defining an activity with Intent Filter (<intent-filter>) in the Android Manifest file (AndroidManifest.xml) like as shown below.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
We can define a filter with multiple instances of <action>, <category> or <data> elements and we need to make sure that component can handle all the combinations of filter elements.
In case if we want to handle multiple Intents with combinations of action, category, and data, we need to create multiple intent filters.
Following is the code snippet of defining multiple Intent filters in the android manifest file to handle multiple Intents.
If you observe above code snippet the activity “MainActivity” will act as an entry point for our app because we defined an activity using MAIN action and LAUNCHER category attributes in intent filters (<intent-filter>).
MAIN - It indicates the app’s main entry point that means it starts the activity which defines with the MAIN action when the user initially launches the app with a launcher icon.
LAUNCHER - It indicates that this activity icon should be placed on the home screen list of apps. In case if the <activity> element doesn’t specify an icon with icon, then the system uses the icon from the <application> element.
These two (MAIN, LAUNCHER) elements must be paired together in order for the activity to appear in the app launcher.
The second activity “ResultActivity” is intended to help us to share the text. The user might enter this activity by navigating from MainActivity and they can also enter directly from another app using Implicit Intent which is matching one of the two activity filters.
Following is the complete example of using Intent Filters in android applications. Here we will configure and send an email using Intent Filters in the android application.
Create a new android application using android studio and open an activity_main.xml file from \src\main\res\layout path. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/sendMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="230dp"
android:text="Send Mail" />
</LinearLayout>
Now open our main activity file MainActivity.java from \src\main\java\com.tutlane.intents path and write the code like as shown below
package com.tutlane.intentfilters;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSend = (Button)findViewById(R.id.sendMail);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent si = new Intent(Intent.ACTION_SEND);
si.setType("message/rfc822");
si.putExtra(Intent.EXTRA_EMAIL, new String[]{"support@tutlane.com"});
si.putExtra(Intent.EXTRA_SUBJECT, "Welcome to Tutlane");
si.putExtra(Intent.EXTRA_TEXT, "Hi Guest, Welcome to Tutlane Tutorial Site");
startActivity(Intent.createChooser(si,"Choose Mail App"));
}
});
}
}
If you observe above code we used multiple components to send email, those are
si - Our local implicit intent
ACTION_SEND - It’s an activity action that specifies that we are sending some data.
setType - We use this property to set the MIME type of data that we want to send. Here we used “message/rfc822” and other MIME types are “text/plain” and “image/jpg”
putExtra - we use this putExtra() method to add extra information to our Intent. Here we add the following things.
Now open android manifest file (AndroidManifest.xml) and write the code like as shown below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.intentfilters">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="message/rfc822"/>
</intent-filter>
</activity>
</application>
</manifest>
If you observe above AndroidManifest.xml file we added following extra fields of Intent filters.
action - we use this property to define that the activity can perform SEND action.
category - we included the DEFAULT category for this activity to be able to receive implicit intents.
data - the type of data the activity can send.
When we run the above program in the android studio we will get the result as shown below.
If you observe the above result when we click on Send Mail button it displays a dialog with the apps which are capable of sending an email. If you observe our app also included in the list.
This is how we can use intent filters in android applications to make our application components can be invoked by other apps.