Day 9: Android Main Concepts: Intents

android-layouts

Over the next three days, we will explore some of the fundamentals of building an Android Application. They are –

  1. Intents
  2. Activities
  3. Services

Today, let’s explore what Intents in Android are –

Intents in Android are just data structures that are message objects. Intents can request an operation to be performed by some other component in Android. Intents are generally used to start Activities and Services in Android. We will explore in coming blog posts on how to use Intents to start an Activity and Services.

All Intents are classified into two Types –

  1. Explicit Intents
  2. Implicit Intents

Explicit Intents

Explicit Intents are where you specify Component that needs to be launched by its class name. The common use case for this is, when, you want to start an activity in your own Android App.

Let’s look at an example of Explicit Intent.

In our Simple Hello World Android Application, let’s add another Layout first –

android-layouts

Let’s also go ahead and add an Activity (You will learn what activities are in the next post, but for now please note that each screen in Android corresponds to an Activity. This Activity will work with the Details Layout that we just added –

 

Gist file link: https://gist.github.com/vkoppaka/bd9cfe6a9f09d77ec67b

Now, let’s go back to our main Activity and write some code to make so that the button click on the activity takes us to newly created Details Activity.

 

Gist file link: https://gist.github.com/vkoppaka/2f179167aea3693761b0

Notice in the button_Click method that we are constructing an Intent and then passing the details of the Activity we are hoping to move to (fully qualified name). And once an Intent is constructed, we just call “StartActivity” method with the intent as a parameter.

intents-samples-1

Main screen above, and once Navigate to Second Screen button is pressed then we move to Details Activity (Second Screen)

intents-samples-2

Implicit Intents

Implicit Intents don’t specify Component but rather define the action that needs to be performed. This actions are then handled by other apps in Android (be it be built-in or third party apps).

Some of the common examples for Implicit Intents are –

  1. Opening a Web Page
  2. Launching Maps
  3. Launching Phone Dialer

Let’s build a quick little sample that demonstrates all these

intents-samples-3

Opening a Web Page

To open a Web Page with Intents, you would use an Intent.ActionView action and pass URL message object. Let’s see it in action –

intents-samples-4

Launching Maps

To launch maps with Intents, you would use Intent.ActionView action and pass a Geo Location message object. Let’s see it in action –

 

Launching Phone Dialer

To launch phone dialer with Intents, you would use Intent.ActionDial action and pass a telephone number message object. Let’s see it in action –

Gist file link: https://gist.github.com/vkoppaka/ecebcace923869a57f30

intents-samples-5

That’s it for today, tomorrow, let’s explore what Activities in Android are in more detail.

 

Get the CODE!
The following two tabs change content below.

Venkata Koppaka

Software Architect at Falafel Software
Venkata Koppaka has over 7 years experience developing variety of line of business applications to various financial and IT organizations. Venkata has been developing with all Telerik products since 2008, he ended working for Telerik on Sitefinity team as a senior developer for Ecommerce module. He is extremely interested in developing custom solutions for Sitefinity based Ecommerce stores. Venkata is a big fan of Test Driven Development and strives himself to write code that is completely testable and modular. Venkata has a Masters in Computer Science from Syracuse University, which drives his continuos learning addiction. In his spare time, Venkata develops apps for Windows Phone 8 platform and learns the most cutting edge technologies in software development. When he is not spending time on computer writing software, Venkata enjoys reading about cars and watching football.