Open In App

Intent Filter in Android with Demo App

Last Updated : 07 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The intent is a messaging object which tells what kind of action to be performed. The intent’s most significant use is the launching of the activity. Intent facilitates the communication between the components.

Note: App components are the basic building blocks of App.

Fundamental use case of Intents

Starting Activity

An activity represents the single screen in an app, Bypassing intent instance we can start an activity.

Example:

Kotlin




var intent = Intent(this, SecondActivity:: class.java)
startIntent(intent)


You can add extra information by using putExtra().

Starting a Service

A Service is a component that performs operations in the background without a user interface, which is also called a background process.

Delivering a Broadcast

A broadcast is a message that any app can receive. In android, the system delivers various broadcast system events like device starts charging, disable or enable airplane mode, etc.

Intent Type

There are two types of intent

  1. Explicit intent: Explicit intent can do the specific application action which is set by the code like changing activity, In explicit intent user knows about all the things like after clicking a button which activity will start and Explicit intents are used for communication inside the application
  2. Implicit Intent: Implicit intents do not name a specific component like explicit intent, instead declare general action to perform, which allows a component from another app to handle.

For example: when you tap the share button in any app you can see the Gmail, Bluetooth, and other sharing app options. 

Intent Filter

  • Implicit intent uses the intent filter to serve the user request.
  • The intent filter specifies the types of intents that an activity, service, or broadcast receiver can respond.
  • Intent filters are declared in the Android manifest file.
  • Intent filter must contain <action>

Example:

XML




<intent-filter
  android:icon="drawable resource"
 Â android:label="string resource"
 Â android:priority="integer" >
   . . .
</intent-filter>


Most of the intent filter are describe by its 

  1. <action>, 
  2. <category> and
  3. <data>.

1. <action>

Syntax:

XML




<action android:name="string" />


Adds an action to an intent filter. An <intent-filter> element must contain one or more <action> elements. If there are no <action> elements in an intent filter, the filter doesn’t accept any Intent objects.

Examples of common action:

  • ACTION_VIEW: Use this action in intent with startActivity() when you have some information that activity can show to the user like showing an image in a gallery app or  an address to view in a map app
  • ACTION_SEND: You should use this in intent with startActivity() when you have some data that the user can share through another app, such as an email app or social sharing app.

2. <category>

Syntax:

XML




<category android:name="string" />


Adds a category name to an intent filter. A string containing additional information about the kind of component that should handle the intent.

Example of common categories:

  • CATEGORY_BROWSABLE: The target activity allows itself to be started by a web browser to display data referenced by a link.

3. <data>

Syntax:

XML




<data android:scheme="string"
      android:host="string"
      android:port="string"
      android:path="string"
      android:pathPattern="string"
      android:pathPrefix="string"
      android:mimeType="string" />


Adds a data specification to an intent filter. The specification can be just a data type, just a URI, or both a data type and a URI.

Note: Uniform Resource Identifier (URI) is a string of characters used to identify a resource. A URI identifies a resource either by location, or a name, or both.

Implementation of Intent Filter with a Demo App

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Add dependencies to the build.gradle(Module:app) file

Add the following dependency to the build.gradle(Module:app) file. We are adding these two dependencies because to avoid using findViewById() in our MainActivity.kt file. Try this out otherwise use the normal way like findViewById().

apply plugin: ‘kotlin-android’

apply plugin: ‘kotlin-android-extensions’

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SEND"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.476"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_bias="0.153" />
  
    <Button
        android:id="@+id/buttonView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.826" />
      
</androidx.constraintlayout.widget.ConstraintLayout>


Step 4: Working with the AndroidManifest.xml File

Following is the code for the AndroidManifest.xml File.

XML




<?xml version="1.0" encoding="utf-8"?>
    package="com.example.menuapplication">
  
    <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/Theme.MenuApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
  
            <!--SEND INTENT FILTER-->
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
  
            <!--VIEW INTENT FILTER-->
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http"/>
            </intent-filter>
  
        </activity>
  
    </application>
  
</manifest>


Step 5: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.intentfilter
  
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // send button on click listener
        sendButton.setOnClickListener {
            var intent = Intent(Intent.ACTION_SEND) // intent
            intent.type = "text/plain"
            intent.putExtra(Intent.EXTRA_EMAIL, "niranjantest@gmail.com")
            intent.putExtra(Intent.EXTRA_SUBJECT, "This is a dummy message")
            intent.putExtra(Intent.EXTRA_TEXT, "Dummy test message")
            startActivity(intent)
        }
  
        // View on click listener
        buttonView.setOnClickListener {
            var intent = Intent(Intent.ACTION_VIEW)
            startActivity(intent)
        }
    }
}


Output with Explanation:

Click on Send Button, you will see a screen like this,

Now choose Gmail app,

Now go to our app and click the view button,

our app dummy app. You can any app from these options because we are using a view intent filter.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads