Android Intent Filters give Appcelerator apps the ability to receive Intents from other Android applications. If you are an Android device user, you’re probably familiar with the SEND Intent. It’s the one that pops up a “Share via” list of apps when you attempt to share links, pictures, videos, etc…
Here we are going to create a very simple app that will make itself available to a very specific Intent via Android Intent Filters. The app will present itself in the Intent list anytime we click on a link that is destined to http://www.appcelerator.com. This app was inspired by a Q&A entry that requested this exact behavior.
Let’s first look at the code for our app. We’re simply going to display a label to the user that tells them we know they just clicked on a link to www.appcelerator.com. Nothing fancy here.
If you build and run it, your output should look roughly like this:
In order for our Appcelerator app to leverage Intent Filter functionality, we need to make a few changes to our AndroidManifest.xml. As you may or may not know, unless you define a custom AndroidManifest.xml file for your project, Titanium Studio will generate a new one for you on each build. Here’s how you create a persistent manifest that you can manage yourself.
- Create a default mobile project in Titanium Studio.
- Replace the existing app.js code with the code above.
- Build and run your app on either an Android device or emulator.
- In your project’s root directory create a
- Go to your project’s
build/androiddirectory and copy AndroidManifest.xml
- Paste the AndroidManifest.xml into your newly create
In the end, your project structure in Titanium Studio should look something like this:
OK, now you have a custom AndroidManifest.xml that will persist between builds. We can now make the necessary changes to allow it to receive Intents from other Android apps.
Open up your custom AndroidManifest.xml and find the main activity for your app. It should be directly under the
<!-- TI_APPLICATION --> comment. As you can see, it already includes an Intent Filter for launching the app. We are going to add one more to handle links to http://www.appcelerator.com.
Very briefly, the
data subelement tells the
intent-filter that we are handling for HTTP requests targeted at the host www.appcelerator.com. The 2
category subelements tell Android that we want to receive implicit Intents and that they can come from the browser, respectively. Finally,
action states that we will be handling VIEW intents. For more information on the specifics of Intents and Intent Filters, be sure to check the Android developer documentation on the subject.
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. Now you can test the Intent Filter by doing the following:
- Open your Android browser
- Search for “appcelerator” on google.com
- 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 app containing the “I’ll bet you just clicked on a link to www.appcelerator.com” label.
Download the full Intent Filter Test project at the Appcelerator Documentation Examples Github repository