Appcelerator Blog

The Leading Resource for All Things Mobile

Android Intents

0 Flares 0 Flares ×
Editor’s note: This blog post is a basic tutorial. The most up-to-date version is available in the wiki.

In our previous article on Android Intent Filters we showed you how you can have your Appcelerator app receive Intents from other Android apps. This time we’re going to show you how you can send your own Intents to other Android apps. This is powerful native functionality that lets us leverage currently installed apps to send, share, view, edit, and perform other operations.

For this example we’ll show a very common and effective use of Intents. We’re going to use an Android Intent to share text with any apps on your Android device that accept text Intents. For example, let’s say you open your Android browser and highlight some text. If you tap that highlighted text, you’ll have the opportunity to share that text with other Android apps, in this case the Google+ app.

So let’s see how we can do this in our own Appcelerator apps. Here is the code snippet where all the magic happens.

Now let’s break it down to understand exactly what’s going on here. You’ll quickly find that much of the details are not so much Appcelerator configurations as they are native Android.

var intent = Ti.Android.createIntent({
    action: Ti.Android.ACTION_SEND,
    type: "text/plain"
});

Above we create the Intent using the Appcelerator API. In this instance we utilize 2 properties

  • action – Tells Android what action we are going to perform with our Intent. In this case we want to send something, as signified by Ti.Android.ACTION_SEND. For a full listing of supported Intent actions, check the Titanium.Android API docs. Also, as these correspond directly to native Android Intent actions, you should also reference the Android Intent documentation for detailed explanations of each.
  • type – This is a MIME type that tells Android what type of data we are planning to send. In our case we just want to send text, so we use text/plain. See the Android Intent function setType() for more details.
intent.putExtra(Ti.Android.EXTRA_TEXT, 'Some text that we want to share');

In the line above we add the text we want to share as an Extra to the Intent. Extras allow as to pass specific data along with an Intent in a standardized format that can be used by any app potentially receiving the Intent. So in our first example, when you sent highlighted text from the browser, that text was wrapped in an Intent Extra which can then be processed by the receiving app. In our case, it added text to the update field in Google+.

We pass 2 parameters to this function:

  • name – This signifies the type of Extra we are sending. As you might imagine, Ti.Android.EXTRA_TEXT signifies that we are adding text. As with the actions, a complete listing of extras can be found with the Titanium.Android API docs and Android Intent documentation.
  • data – This is the actual data being sent with the Intent via this Extra. Here we just use a string constant, but you can obviously use a string from anywhere in your app. In the included sample app, we’ll use a Ti.UI.TextArea for this parameter.
intent.addCategory(Ti.Android.CATEGORY_DEFAULT);

Above we specify the category of Intent Filters to which this Intent is targeted. We’ll be using Ti.Android.CATEGORY_DEFAULT which essentially does not use any category filtering. Once again, a complete listing of categories can be found with the Titanium.Android API docs and more in depth descriptions are in the Android Intent documentation.

Now our Intent is prepared to send text to another Android app. All we need to do is tell the current Android Activity that we want to send the Intent. To do so, we use the following line of code.

Ti.Android.currentActivity.startActivity(intent);

Once this code executes you will see one of 2 things:

  1. If you are running in the Android emulator, you’ll likely see your default messaging app pop up as you don’t have any other apps that receive text Intents.
  2. If you are running on an Android device, you should see an Intent list that contains all the apps on your device that accept text Intents. Mine looks like the one on the right.

Here’s a few examples of what the Intent we just sent will look like when it is received by some of Android’s most popular apps. As you can see, you get incredible power and integration using native Android Intents for less than 10 lines of code!

Sample App

Helpful Links

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×