Android Intent

Web Hosting
As the name suggests, intents are a way of telling Android what you want to do. In other words, you describe your intention. Intents are usually used to launch an Activity or Service. They are also used to trigger broadcast receivers. Intents do not only describe what you want to do, they also can have extra data attached to them. I will go over some examples of how to define an intent and then how to use them.

Defining Intent

There are 2 types of intents. Explicit and implicit intents.
Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.
For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityand then use it to directly call it.
Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use.
For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.

Explicit Intent Definition

//Set the target of the intent to ActivityB
Intent intent = new Intent(this, ActivityB.class);
//Set the data extra to mURI with a key mExtraKey
intent.putExtra(extraKey, mUri);

First we create a new intent and we pass the application’s context and the target that we want to call to the constructor.
Next we set the extra of the intent. In this case it’s a URI. Each extra should have a key to identify it so that it can be retrieved by the receiving end later on. Keys are String data types.

Implicit Intent Definition

//Create an empty intent
Intent intent = new Intent();

//Set the action of the intent to ACTION_SEND

//Set the data extra to mUri with a key extraKey
intent.putExtra(extraKey, mUri);

//Set the type of the extra to image

First we create an empty intent. Then we set the action type to ACTION_SEND to tell Android that we want to launch a component that can send the extra data.
Setting the extra is the same as in explicit intents. We also set the data type to “image/*” so that Android can pick the most appropriate component to launch.

Using Intents

As I mentioned before, Intents can be used to launch different components. Activities, Services and trigger broadcast receivers. I will not go over what each of these components are used for, but I will show you examples of how to use intents to launch each one.

Launching an Activity

//Start an activity with an explicit or implicit intent

Launching a Service

//Start a service with an explicit or implicit intent

Triggering a Broadcast Receiver

//Send a broadcast with an explicit or implicit intent

Other possibilities

Keep in mind that this is not all. You can use intents in a much more flexible way. There are many flags that you can set that define different behaviors. There is also a large list of predefined actions that the system can handle. I suggest you check out the Intent documentation at the official Android developer’s website.

Web Hosting

No comments:

Post a Comment