Intent Filters in Android

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” 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 video and clicked it, so does your application will launch and play this video? No, if you’d didn’t specify about app component in intent fillers.

what is android Intent Filters examples

The upper diagram shows layout presentations, how 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 have some data like PDF file and want to open it. So it can just pass data in intent with set of <action> <category> <data>. Now Android system will search for same in the system to which 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>

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

 

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

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.

 

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.

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.

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.

 


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.