INTENTS
By: Steven Cen
CONTENTS
MINI LESSON
To understand what an intent is, you must know what an activity is, which is pretty simple. An activity is a single “screen” and an interface within an application, like the inbox activity of Gmail (pictured below), or the login screen of Facebook. When you click on an email in the inbox, it brings you to a new activity which contains the full content of the email, and when you press the back button on your device, it brings you to the activity which you were previously viewing, which in this case is the inbox. Android apps can be made of a single activity like the simple calculator you made, or for more complex apps, made of many.
How does an Intent fit into the picture here? An intent is the way one activity can start another activity. You can think of it like this: the first activity has the “intention” to start a new activity, so it uses an intent. For example, when you click on an email in the inbox activity, Gmail sends an intent to the activity which is able to view the full contents of that email.
A lot of the time, extra pieces of data can be loaded in an intent, like the string “hi, lol.” With this string loaded onto an Intent, you can send the intent to Facebook to be posted. Or, when you select a photo from your Photos app to be shared on Hangouts via an Intent with data including the photo.
There are two main types of intents – explicit and implicit intents.
Explicit:
Implicit:
Parts of an Intent
An intent is composed of several parts which determine whether it is implicit or explicit, and the data the intent can carry.
The ones we care about for now are listed below:
PRACTICE
Share a Piece of Text via an Implicit Intent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/share_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Type
whatever you want to tell the world"
android:textSize="30sp">
</EditText>
<Button
android:id="@+id/share_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Share!"/>
</LinearLayout>
android:onClick="shareWithIntent"
public
void shareWithIntent(View v) {
//Get the content of your EditText
String content = shareEdit.getText().toString();
//Declare a new intent, with an action (look above if you forget
what an “action” is) that tells the app to "send," aka share
something.
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
//Set the Type of the intent to be plain text. Other types include
images and audio files.
sharingIntent.setType("text/plain");
//An Extra is the data you want to sent along with the intent.
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
//Start the new activity by an implicit intent, using the intent
you just packaged.
startActivity(sharingIntent);
}
Open a link in a browser
android:inputType="textFilter|textNoSuggestions"
<Button
android:id="@+id/browser_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open in browser"
android:onClick="browserIntent"/>
public
void browserIntent(View v) {
//get your URL from the textbox
String url = shareEdit.getText().toString();
//If you don't type "http://" or "https://" in
your EditText, this code will prefix it
if (!url.startsWith("http://") &&
!url.startsWith("http://")) {
url = "http://"
+
url;
}
//Create a new intent with the action ACTION_VIEW (something to
view -- a website from a URL)
Intent urlIntent = new Intent(Intent.ACTION_VIEW);
//Set the data of the intent to the url you entered
urlIntent.setData(Uri.parse(url));
//Start the implicit intent -- Android will now look for all
browsers with activities on your device which are capable of opening your URL
startActivity(urlIntent);
}
Google Maps API with Explicit Intent
<Button
android:id="@+id/maps_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search for locations nearby"
android:onClick="mapsIntent"/>
public
void mapsIntent(View v) {
//get your search query from the EditText
String query = shareEdit.getText().toString();
//Search for whatever your query was nearby your current location
Uri searchUri = Uri.parse("geo:0,0?q=" + query);
//Create an intent with the Action of viewing
Intent searchIntent = new Intent(Intent.ACTION_VIEW, searchUri);
//Make this an explicit intent to Google Maps and only Google Maps
searchIntent.setPackage("com.google.android.apps.maps");
//Launch Google Maps with this search query
startActivity(searchIntent);
}
Recommended Comprehensive Tutorial:
http://www.vogella.com/tutorials/AndroidIntent/article.html