Intents are used in many ways. But, what it is an Intent? If we go to the Google Android Developers Reference we can find this definition:
“An intent is an abstract description of an operation to be performed”
What operations are we talking about? As mentioned in this articles header we can use Intents to pass information from one activity to another, this is just one functionality, we bind these two Activities…but we can bind services with Activities too! With Intents we can even make a service to update information in an Activity. But, what other operations can we perform with Intents?
I gave a little introduction of intents in the How-to create an Address Book in the Google Android Programming Environment – Finishing the Application , we create intents to perform the “Call to” action or the “Send a SMS to” action. Just as easy as:
Intent i = new Intent();
i.setData(Uri.parse(“tel:” + phoneNumber));
for the “Call to” action.
Further in this article, we will see more examples of useful intents.
I called them “binding intents” just to point that we are going to talk about Intents that binds Activities, so we can pass information from one place to another.
Lets think of these intents using two examples:
-Example A: We need to give some information to a new Activity we are going to create now. This information will be for this example a phone number. In your first Activity you click a contact from a list of contacts, you then want to open a new window with this contact information in a new and amazing User Interface.
In the ActivityA.java
Intent intent = new Intent(“name.of.the.ActivityB”);
We create a new Intent, using the name of the ActivityB we have set in the AndroidManifest.xml.
<action android:name=“name.of.the.ActivityB” />
Where in “name.of.the.ActivityB” you can set the value you want.
We put the info we want to send in the “putExtra” method, we set a name to remember “INFO_TO_PASS” in this example, and the concrete info we want in the ActivityB.
In the ActivityB.java
Bundle extras = getIntent().getExtras();
variableGet = extras.getInt(“INFO_TO_PASS”);
Here we create a new Bundle object to get the information from the Intent. We can use “getInt”, “getString”, “getByte” to get the information. This method is similar to the GET and POST methods when we work in web programming environment.
More in Part II
In our next article we will explain the second example of the Intent application, and we will have a look at very useful intents!! Stay tuned.
This post is part of the series: How-to develop Google Android Applications - Going further
Here more functionality and simple how-to guides to show you how to work with Google Android