Skip to content

Intent Filters in Android | Example code

  • by

Intent Filters definition in easy words is Filtering the operation and perform an Action to deliver the target component.

The intent is “Explicit Intent” with the component name field set, then it’s delivered to the target component. But in “Implicit Intent” which is typical for communication between applications, android must determine the matched activity or service (or set of receivers) on its own. How does Android do that? That’s where intent filters come in.

Let see the example you build apps for play videos and published store. Users installed and using it’s. But if someone has a video and clicked it, so does your application will launch and play this video? No, if you’d didn’t specify about the app component in intent filters.

what is android Intent Filters examples

The upper diagram shows layout presentations, how the Intent and Intent filter works. Let understand Application 2 and Application 4 has defined some step of the rule in Intent filters (In manifest). Now another Application 1 has some data like a PDF file and wants to open it. So it can just pass data in intent with set of <action> <category> <data>. Now Android system will search for the same in the system to which the application has configured to take the input of that type <action><category> <data>. After found matching, it will give you the option to choose any one apps.

An intent filter declares (define some rule or attributes) the capabilities of its parent component (Activity, Service, Broadcast receiver etc), what activity or service or broadcast receiver can do and what types can handle.

Before going in deep must read about What is an Intent and its example.

Syntax :  <intent-filter>

<...>
<intent-filter android:icon="drawable resource"
               android:label="string resource"
               android:priority="integer" >
    <action> .... </>
    <category> ... </>
    <data> ... </>
</intent-filter>
</...>
what is Intent Filters and example demonstration

Contained in :

<activity> ,<activity-alias> , <service>  ,<receiver>

Must contain:

<action>

Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

some actions are ACTION_CALLACTION_MAINACTION_BATTERY_LOW

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

Optional contain:

<category>

Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

Some categories: CATEGORY_BROWSABLE, CATEGORY_LAUNCHER

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

Note: To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methodsstartActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULTcategory. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.

“Official Google”

<data>

Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (schemehostportpath) and MIME type.

<data android:mimeType="text/plain"/>

Let’s build one simple Intent filters example

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Add Button widget in the main_activity.xml resource layout file

The button is for performing an operation.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="72dp"
        android:text="Go To Tutorial"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>
Step 3. Add the following code in MainActivity.class

Set on Click listener in button and Create Intent instance Pass the Action Intent.ACTION_VIEW and set Data Uri.parse(url).

Now when you click the button its send intent in an Android system where it will search matching component define Intent filters. If there is more then one result then it will show multiple option otherwise open on only one.

package in.eyehunt.intentfilters;

import android.content.Intent;
import android.net.Uri;
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 button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = "http://tutorial.eyehunts.com";
                //action
                Intent i = new Intent(Intent.ACTION_VIEW);
                //data
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });
    }
}
Step 6. Run the application, in an emulator or On you android device
Output screenshot Android Intent Filters example :
Output screenshot Android Intent Filters example

Video Output

Download source code Intent-filters example :

https://github.com/EyeHunts/IntentFilters

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Java

Same as this example you can build your own app for browsing and define components (activity) as a target.

Bouns : 

Even you are not aware you can check your AndroidManifest.xml file, where your main activity also using an Intent filter. Without Define <action> and <category> your app will not launch because the Android system doesn’t know about which activity is the entry point of your app.

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

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading