Droid Tips: Useful Intents
I recently built a company directory app for our company. It was really more of a proof of concept than a sell to the world kind of app, but it still required the same amount of effort to get it built. Three of the things I wanted the app to do were email, call, and text to each of the entries in the directory. Along the way, I came across an amazingly easy way of pulling this off using Intents.
Intents are easy to work with and have the added benefit of not closing your current application. Once the Intent is called, your app is simply moved to the background until you have finished.
If you would like a full description on uses and available Intents, please refer to the developers guide found on the droid site. There you will find a full description of the Intent Object as well as related methods.
Each Intent consists of three main parts, the actual intent you want to send, extra information you’d like to pass through the intent, and running the intent. You will also need to update your manifest file with permission for each entry. I will list the intent code and the permission line needed for each.
Emailing Intent
// Permission needed in manifest file
<uses-permission android:name="android.permission.INTERNET" />
// Actual Intent
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{“test@test.com”});
startActivity(Intent.createChooser(emailIntent, "Select email application."));
The first line is the creation of the intent. For sending an email, we shall use the ACTION_SEND. This will point it to an email intent and look for the correct extras.
The second and third lines are used to specify what specific information we would like to pass to the email. The first, setType, will be the MIME type for the email. The second, putExtra, is where you can put the email in which you would like to send. EXTRA_EMAIL indicates what emails you would like to send to. You may put in more than one. Other possible extras you may pass are EXTRA_TEXT, EXTRA_CC, EXTRA_BCC, and EXTRA_SUBJECT.
The last line is where the actual work is done. This one is done a bit different than the other two in that you need to first call the createChooser method first. This will pull up a list of the current programs on their phone that can do the email, and then populate based on whatever application they choose. That’s it.
Texting Intent
When I started researching this, I found many articles on how to recreate and send texts right from your application. If you would like to do that, an article on mobiforce.com is a good example. For the scope of my app, I did not need to go to that depth.
// Permission needed in manifest file
<uses-permission android:name="android.permission.SEND_SMS"/>
// Actual Intent
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", “701-555-1234”);
smsIntent.putExtra("sms_body", “SMS Text”);
startActivity(smsIntent);
Similar to the Emailing intent, the first line simply calls our action needed, ACTION_VIEW. The second and third lines are the ‘extra’ information. The type is a built in android type to identify as being a text message. The extras are the phone number you wish to send to and any text you’d like to add to the body of the text. The last is what triggers the intent.
Calling Intent
// Permission needed in manifest file
<uses-permission android:name="android.permission.CALL_PHONE"/>
// Actual Intent
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(“tel:701-555-1234”));
startActivity(callIntent);
This intent is probably the easiest of the three since there is not much extra information you can send with it. The first line is our intent, ACTION_CALL. The second is the URI version of our number to call. One note is that the number itself starts with ‘tel:’. The last line, as with SMS messages is what triggers the call.
Intents cover a lot more ground than I’ve listed here, but these three are common jobs that your phone is used for. I hope this code helps you keep your app small, yet powerful.

Comments
Be the first to comment!
Leave A Comment