What Are Intent-Filters in Android

What are intent-filters in Android?

When there are multiple activities
entries in AndroidManifest.xml, how
does android know which activity to
start first?

There is no "first". In your case, with your manifest as shown, you will have two icons in your launcher. Whichever one the user taps on is the one that gets launched.

I could not understand intent-filters.
Can anyone please explain.

There is quite a bit of documentation on the subject. Please consider reading that, then asking more specific questions.

Also, when you get "application has stopped unexpectedly, try again", use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine the Java stack trace associated with the error.

What are Intent Filters exactly?

I think it's well documented here. Summarizing, when (e.g.) you pass an intent to Context.startActivity, or Context.startService, or broadcast it with Context.sendBroadcast, etc, what activity or service (or, what set of broadcast receivers) is the intent delivered to?

Answer: if the intent is "explicit", i.e., it has the component name field set, then it's delivered to the target component it designates -- this typically happens in communication within an application.

Otherwise ("implicit" intent, as is typical for communication between applications), Android must determine the best activity or service (or set of receivers) on its own. How does Android do that? That's where filters come in, and I quote:

It does so by comparing the contents
of the Intent object to intent
filters, structures associated with
components that can potentially
receive intents. Filters advertise the
capabilities of a component and
delimit the intents it can handle.
They open the component to the
possibility of receiving implicit
intents of the advertised type. If a
component does not have any intent
filters, it can receive only explicit
intents. A component with filters can
receive both explicit and implicit
intents.

The web page in question continues with many details and, at the end, a complete, simple example, and I think it would be redundant to copy and paste them here.

What is the use of intent-filter in the manifest for a service?

As mentioned in the other answers, the intent filter helps your service classify the type of action to execute. So for example, you can try starting the service like below:

Intent intent = new Intent(this, MyService.class);
intent.setAction("INTENT_SAMPLE");
startService(intent);

Then in MyService class, check for the action like so:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if("INTENT_SAMPLE".equals(intent.getAction())) {
// do INTENT_SAMPLE action here
}
else {
// do non INTENT_SAMPLE action here
}
}

Since https://developer.android.com/guide/components/intents-filters#Types does not recommend intent filters for services, drop the action from manifest. To achieve the same result as above, simply add an extra to your intent like so:

Intent intent = new Intent(this, MyService.class);
intent.putExtra("INTENT_SAMPLE", true);
startService(intent);

Then in MyService class, check for the action like so:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent.getBooleanExtra("INTENT_SAMPLE", false)) {
// do INTENT_SAMPLE action here
}
else {
// do non INTENT_SAMPLE action here
}
}

Cheers!

Android: How Intent-Filter works?

Intent filters are supposed to be added BETWEEN the opening and closing tags of a receiver, service or activity. They signify the "implicit intents" that the app can handle. In your app menu, where you have all your apps listed, android looks for the intent Main and Launcher. Whichever apps have that as an intent filter, those get displayed, and the activity associated with Main, Launcher gets called as soon as the user opens the app.

      <activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

These two intent filters associated with my activity called MainActivity tell android to 1) Place my app in the menu. 2) Open MainActivity as soon as the user selects the app. Hence you should have only one activty with Main and Launcher as its intent filters.

For example if user selects share button and its an implicit intent, then the apps that have "share option" in the form of an filter can be called via a dialog box/ selector.

EDIT :

<

activity android:name="ShareActivity">
<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/vnd.google.panorama360+jpg"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
</intent-filter>
</activity>

The first activity, MainActivity, is the app's main entry point—the activity that opens when the user initially launches the app with the launcher icon:

The ACTION_MAIN action indicates this is the main entry point and does not expect any intent data.
The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in the system's app launcher. If the <activity> element does not specify an icon with icon, then the system uses the icon from the <application> element.

These two must be paired together in order for the activity to appear in the app launcher.

The second activity, ShareActivity, is intended to facilitate sharing text and media content. Although users might enter this activity by navigating to it from MainActivity, they can also enter ShareActivity directly from another app that issues an implicit intent matching one of the two intent filters.

http://developer.android.com/guide/components/intents-filters.html
Take a look at this site. So intent filters describe what the activity CAN do, how it CAN be started (via another app or the main launcher or a browser) and what additional functions it can do.

Android: Understanding Intent-Filters

Instead of looking at it from your app's point of view, flip it around and look at it from the Intent side.

When an Intent is created, the creator has no idea what apps are on the system to handle that Intent. But the creator does know what it wants to do (e.g., an app might want to let the user pick out a contact from somewhere on the device), and needs to reach out to other apps on the system to ask for what's desired.

To do this, Intents have several pieces of information attached to them. Among them are actions and categories.

The actions define in a general way the action the Intent wants to do, like VIEW a contact, PICK an image from the Gallery, etc.

The category is an additional piece of information that gives the Intent another way to differentiate itself. For example, when a link in the browser is clicked, the Intent that is created has the BROWSABLE category attached to it.

So, when the OS resolves the Intent, it will look for registered Activities or BroadcastReceivers that have an intent filter that includes all of pieces of information. If the Intent specifies the PICK action, Activities that do not have an intent-filter with the PICK action will be discarded from the list of candidates to handle the Intent.

In this way, the combined set of action, categories, type, and (possibly) scheme associated with an Intent serve to pinpoint the set of Activities that can handle the Intent. When you set up your intent-filter in your manifest, you are telling the OS which class of Intents you can handle.

Android Manifest- intent filter and activity

android:name=".AboutUs"

This is the name of your Activity class, the dot at the front is shorthand notation for your package. So this actually stands for com.your.package.name.AboutUs which means your java file that represents this Activity is called AboutUs.java

android:label="@string/app_name"

label is the string that gets shown in the launcher(if the activity is listed in the launcher) and at the top of the window when the activity is open.

<intent-filter > ... </intent-filter>

intent filter defines the Intents that your activity "listens for" in order to launch.

<action android:name="com.example.app1.ABOUT" />
<category android:name="android.intent.category.DEFAULT"/>

Action and category are both fields that get set on an Intent before it is "fired off" into the system. The system will then look for any activities that match both the action and category and if it finds one then it will launch that activity, or if it finds multiple it will show the user all of them and let them pick.

In your case your the action you are listening for com.example.app1.ABOUT is a custom action that is specific to your app, not one of the systems actions.

So here is what an intent that would start this particular activity might look like:

Intent i = new Intent();
i.setAction("com.example.app1.ABOUT");
i.addCategory("android.intent.category.DEFAULT");
startActivity(i);

Note that because you've created a custom action, this intent does not require access to your AboutUs.class so this intent could technically be fired from any app on the device and it would launch into your activity.

What does an IntentFilter do?

To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters. Each filter describes a capability of the component, a set of intents that the component is willing to receive.

An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as elements.

A filter has fields that parallel the action, data, and category fields of an Intent object. An implicit intent is tested against the filter in all three areas. To be delivered to the component that owns the filter, it must pass all three tests. If it fails even one of them, the Android system won't deliver it to the component — at least not on the basis of that filter. However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another.

For more information regarding Intent-Filter, refer this Android Developer - Intent Filters Page



Related Topics



Leave a reply



Submit