Would you like to contribute to the Titanium docs? To get started, sign up for an account on the Appcelerator Wiki and sign our CLA.

Skip to end of metadata
Go to start of metadata

Introduction

Android Intent Filters give applications the ability to receive implicit intents from other Android applications. An intent is a message object, usually containing data, sent by another application to start a new application to handle the data.  Intents are sent to the Android OS, which directs the intent to the appropriate application to handle the data.  If more than one application can handle the data, Android presents a dialog to the user to select an application to launch.  An intent filter indicates to the Android OS that your application can handle certain data types or URIs.  For example, if a user opens their browser and highlights some text, the user can share that text with other Android apps, such as an e-mail application, SMS application or a social networking application.

The user highlights the text and
clicks the Share button.

The Share dialog appears and
the user can select the application
to send the text to, such as...
...an e-mail application,...an SMS application...or a social networking
application like Google+.

 

Receive an Implicit Intent

To advertise to the Android OS that the application can receive an implicit intent, declare <intent-filter> elements in the tiapp.xml file for each data type you want the application to receive.

Modify the Android Manifest Section

Before declaring an intent filter, you need to copy the <activity> node containing your application's default root activity from the build/android/AndroidManifest.xml file to the Android manifest section in the tiapp.xml file.

  1. Build your project once to generate the AndroidManifest.xml file.
  2. Open the build/android/AndroidManifest.xml file and and copy the <activity> node that contains your default root activity.  The android:name attribute will contain the name of your application. 

    android/build/AndroidManifest.xml
  3. Paste the default root activity into the Android section of your tiapp.xml file inside the <application> element.  You may need to add the <manifest> and <application> elements to the <android> element of your tiapp.xml file: 

    tiapp.xml

Note that the <activity> node you just copied over already contains an <intent-filter> element.  The elements inside the filter describe to the Android OS that this activity is the initial activity to launch for the application.  Do not remove this intent filter or the application will not be available in the application launcher menu.

Icon

If you change the name of the application, you will need to copy the newly generated android:name value from the AndroidManifest.xml file and overwrite the old value in the tiapp.xml file.

Declare Intent Filters

To declare an intent filter, add <intent-filter> elements as children of the <activity> describing the default root activity of the application.  For each <intent-filter>, you must add:

  • one or more <action> elements to it to describe which actions the activity can perform
  • the <category android:name="android.intent.category.DEFAULT"/> element to allow it to receive implicit intents as well as any additional categories you want to add to describe the purpose of the activity
  • and a <data> element describing which data types the activity can receive

The elements are described in more detail below.

After adding an <intent-filter> tag, add the following children elements to it:

  • <action/> element: Describes which actions the activity can do.  Assign the android:name attribute an action.  An action is one of the ACTION_* constants from the Android Intent class.  Remove the ACTION_ prefix from the constant, then prefix it with the android.intent.action namespace.  Examples:
    • <action android:name="android.intent.action.SEND"/>: Data can be sent to the activity.
    • <action android:name="android.intent.action.VIEW"/>: Data can be viewed by the activity.
  • <category/> element: Provides additional details about the activity.  Assign the android:name attribute a category.  A category is one of the CATEGORY_* constants from the Android Intent class.  Remove the CATEGORY_ prefix from the constant, then prefix it with the android.intent.category namespace.  Examples:
    • <category android:name="android.intent.category.BROWSABLE"/>: Indicates that the activity can be invoked from a browser by clicking a link

  • <data/> element: Describes the data type the activity can handle.  You can either describe the data type as a MIME type with the android:mimeType attribute or as an URI using the android:schemeandroid:hostandroid:portandroid:pathandroid:pathPrefix and android:pathPattern attributes that take on the form: <scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]. Note that all URI attributes are optional except if the scheme attribute is not defined, all other URI attributes are ignored, and if the host attribute is not defined, all other URI attributes except scheme are ignored.  Examples:
    • <data android:mimeType="image/*"/>: Activity can accept intents with the image/* MIME type depending on the action.
    • <data android:scheme="http" host="www.fakedomain.com"/>: Activity can accept URIs that start with http://www.fakedomain.com depending on the action.
Icon

For the actions and categories, you can also define your own custom action and category names. Use a reverse domain scheme to name the actions and categories to avoid potential conflicts, for example, com.appcelerator.action.LINT or org.foo.category.SUPER. The custom actions and categories are only useful to communicate between your applications and application activities using intents.

Additionally, you can overwrite the appearance of the option in the Share dialog by using the following <intent-filter> attributes:

  • android:icon: Icon to display in the Share dialog.  Place the icon file in the /platform/android/res/drawable/ folder and reference the image file by removing its extension and prefixing it with @drawable/, for example, @drawable/filename. By default, uses the application's icon.
  • android:label: Title displayed in the Share dialog.  By default, uses the application name.
  • android:priority: Integer used to determine the priority to give to the application. By default, set to 0.  Can be assigned a value between -1000 and 1000, inclusive.

 

tiapp.xml

 

Retrieve Data Passed to the Activity

To retrieve data sent to an activity using an intent, the application needs to retrieve the intent that launched the activity, then use the Intent reference to get the data URI or extras sent with the intent.

  • To get the intent of the root activity, use the Titanium.App.Android.launchIntent property.
  • To get the intent of an activity that has an open Window or TabGroup, get a reference to the activity using the activity property of the Window or TabGroup object, then access the intent using the activity's intent property or getIntent() method.
  • To get the intent of other activities, use the Titanium.Android.currentActivity property to get a reference to the activity, then access the intent using the activity's intent property or getIntent() method.


To retrieve the data URI sent with the intent, simply access the data property of the intent or call the intent's getData() method.

 

 

To retrieve extras sent with the intent, call the intent's hasExtra() method and pass it the property to retrieve to verify the data exists, then call one of the intent's get*Extra() methods and pass it the same property name to retrieve the data.

The example below checks for EXTRA_TEXT data sent with the intent.

 

Respond to the Calling Activity

To send a response back to the calling activity, the called activity needs to call its setResult() method, then call the finish() method to close the called activity in order to send the result.

Icon

The calling activity needs to call the startActivityForResult() method instead of the startActivity() in order to process the result. See Android Intents: Start an Activity with an Intent.

Pass a Titanium.Android.RESULT_* constant to the setResult() method to indicate the result of the activity and an optional intent object as the second parameter.  Use the intent object to send data back to the calling activity.

The example below returns a string once the activity is created.

someactivity.js

MIME Type Intent Filter Example

This example creates an Alloy application that will accept a string from an external application to set the text in a text area widget.

The view code for the application displays a text area and button.

app/views/index.xml
app/styles/index.tss

The controller code retrieves the text string sent to the application if an external application used an intent to start it.

app/controllers/index.js

Now build the application once to generate the AndroidManifest.xml file.

Next, declare the intent filter in the tiapp.xml file.

  1. Copy the default root activity from the AndroidManifest.xml file to the Android manifest section of the tiapp.xml.
  2. Add an <intent-filter> element as a child of the <activity> element.
  3. Add a <data> element as a child of the <intent-filter> element, then assign text/plain to the android:mimeType attribute to tell Android the activity can handle plain text data.
  4. Add a <category> element as a child of the <intent-filter> element.  Assign android.intent.category.DEFAULT to the android:name attribute to tell Android that the activity wants to receive implicit Intents.
  5. Add an <action> element as a child of the <intent-filter> element, then assign android.intent.action.SEND to the android:name attribute to tell Android that the application can receive plain text data.

Note that the activity already had an intent filter declared indicating that the application should be available in the application launcher.

tiapp.xml

You now have an app that is ready to receive Intents. Install it on either an Android device or emulator and make sure it runs. Close the application.  Now you can test the Intent Filter by doing the following:

Open your Android browser and go to a website.
Select some text, then click the Share button.

You should be greeted with an Intent list that
looks something like this.
Select your app in the list and you will then be directed
to your application containing the string you highlighted.

URI Intent Filter Example

This example creates a very simple application that will make itself available to a very specific intent. The application will present itself in the intent list anytime we click on a link that is destined to http://www.appcelerator.com.

The code for the application simply displays a label to the user that tells them we know they just clicked on a link to www.appcelerator.com.

Resources/app.js

If you build and run it, your output should look roughly like this:

Next, declare the intent filter in the tiapp.xml.

  1. Copy the default root activity from the AndroidManifest.xml file to the Android manifest section of the tiapp.xml.
  2. Add an <intent-filter> element as a child of the <activity> element.
  3. Add a <data> element as a child of the <intent-filter> element, then assign http to the android:scheme attribute and www.appcelerator.com to the android:host attribute.  This tells Android the activity can handle HTTP requests targeted at the host www.appcelerator.com.
  4. Add a <category> element as a child of the <intent-filter> element.  Assign android.intent.category.DEFAULT to the android:name attribute to tell Android that the activity wants to receive implicit Intents.
  5. Add another <category> element.  Assign android.intent.category.BROWSABLE to the android:name attribute to tell Android that the request can come from the browser.
  6. Add an <action> element as a child of the <intent-filter> element, then assign android.intent.action.VIEW to the android:name attribute to states that the activity will be handling VIEW intents.

Note that the activity already had an intent filter declared indicating that the application should be available in the application launcher.

tiapp.xml

You now have an app that is ready to receive Intents. Install it on either an Android device or emulator and make sure it runs. Close the application. Now you can test the Intent Filter by doing the following:

  1. Open your Android browser.
  2. Search for "appcelerator" on google.com.
  3. Click the link for http://www.appcelerator.com.

You should be greeted with an Intent list that looks something like this:

Select your app in the list and you will then be directed to your application containing the "I'll bet you just clicked on a link to www.appcelerator.com" label.

Further Reading

Android Developers: Intents and Intent Filters

Android Developers: <intent-filter>

Appcelerator Enterprise Mobility Blog: Android Intents