Intent Filter in Android with Demo App
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
An activity represents the single screen in an app, Bypassing intent instance we can start an activity.
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.
There are two types of intent
- 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
- 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.
- 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>
Most of the intent filter are describe by its
- <category> and
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.
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.
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.
Step 4: Working with the AndroidManifest.xml File
Following is the code for the AndroidManifest.xml File.
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.
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.