KIO
Kreative Ideen online
Intents

Intents

Whenever you want an activity to start a second activity, you use an intent. You can think of an intent as an “intent to do something”. It’a a type of message that allows you to bind separate objects (such as activities) togehter at runtime. If one activity wants to start a second activity, it does it by sending an intent to Android. Android will start the second activity and pass it the intent.

It starts the root activity of a task via an intent (a kind of message) that Android sends when the user has tapped the icon of the respective app.

You start an activity by creating an intent and using it in the startActivity() method.

Your can create and send an intent using just a couple of lines of code.You start by creating the intent like this:

//The intent specifies the activity you want to receive it. It’s like putting an adress on an envelope
Intent intent = new Intent(this, Target.class);

The first parameter tells Android which object the intent is from: you can use the word this to refer to the current activity.
The second parameter is the class name of the activity that needs to receive the intent.
Once you’ve created the intent, you pass it to Android like this:
//startActivity() starts the activity specified in the intent.
startActivity(inent);

This tells Android to start the activity specified by the inten.

You’ve already seen how you can use an intent to start a second activity within the same app. The first activity passes an intent to Android, Android checks it, and then Android tells the second activity to start.

The same principle applies to activities in other apps. You get an activity in your app to pass an intent to Android, Android checks it, and then Android tells the second activity to start even though it’s in another app. As an example, we can use an intent to start the activity in Gmail that sends messages, and pass it the text we want to send. That means that instead of writing our own activities to send emails, we can use the existing Gmail app.

This means that you can build apps that perform powerful tasks by chaining together activities across the device.

But we don’t know what apps are on the user’s device

There are three questions we need answers to before we can call activities in other apps:

  • How do we know which activities are available on the user’s device?
  • How do we know which of these activities are appropriate for what we want to do?
  • How do we know how to use these activities?

The great news is that we can solve all of these problems using actions. Actions are a way of telling Android what standard operations activities can perform. As an example, Android knows that all activities registered for a send action are capable of sending messages.

So far you’ve seen how to create an intent that launches a specific activity using:

This intent is an example of an explicit intent; you explicitly tell Android which class you want it to run.

//We’ve told the intent which class it’s intended for, what if we don’t know?
Intent intent = new Intent(this, SomerandomActivity.class);

If there’s an action you want done but you don’t care which activity does it, you create an implicit intent. You tell Android what sort of action you want it to perform, and you leave the details of which activity performs it to Android.

How to create the intent

You create an intent that specifies an action using the following syntax:

Intent intent = new Intent(action);

where action is the type of activity action you want to perform. Android provides you with a number of standard actions you can use. As an example, you can use …

  • Intent.ACTION_DIAL – to dial a number
  • Intent.ACTION_WEB_SEARCH – to perform a web search
  • Intent.ACTION_SEND – to send a message

So, if you want to create an intent that specifies you want to send a message, you use:

Intent intent = new Intent(Intent.ACTION_SEND);

You can find out more about the sort of activity actions you can use and the extra information they support in the Android developer reference material: http://tinyurl.com/n57qb5

Adding extra information

Once you’ve specified the action you want to use, you can add extra Information to it. We want to pass some text with the intent that will form the body of the message we’re sending. To do this, you use the following lines of code for example:

intent.setType(“text/plain”);
intent.putExtra(Intent.EXTRA_TEXT, messageText);

where messageText is the text you want to send. This tells Android that you want the activity to be able to handle data with a MIME data-type of text/plain, and also tells it what the text is.

You can make extra calls to the putExtra() method if there’s additional information you want to add. As an example, if you want to specify the subject of the message, you can use:

// If subject isn’t relevant to a particualar app, it will just ignore this information. Any apps that know how to use it will do so.
intent.putExtra(Intent.EXTRA_SUBJECT, subject);

When Android is given an intent, it has to figure out which activity, or activities, can handle it. This process is known as intent resolution.

When you use an implicit intent, Android uses the information in the intent fo figure out which components are able to receive it. It does this by checking the intent filters in every app’s copy of AndroidManifest.xml.

An intent filter specifies what types of intent each component can receive. As an example, here’s the entry for an activity that can handle an action of ACTION_SEND. The activity is able to accept data with MIME types of text/plain or image:

<activity android:name=”ShareActivity”>
<intent-filer>
<!– This tells Android the activity can handle ACTION_SEND –>
<action android:name=”android.intent.action.SEND” />

<!– The intent filter must include a category of DEFAULT of it wont be able to receive implicit intents –>
<category android:name=”android.intent.category.DEFAULT” />

<!– These are the types of data the activity can handle –>
<data android:mimeType=”text/plain” />
<data android:mimeType=”image/*” />
</intent-filter>

</activity>

The intent filter also specifies a category. The category supplies extra information about the activity such as whether it can be started by a web browser, or whether it’s the main entry point of the app. An intent filter must include a category of android.intent.category.DEFAULT if it’s to receive implicit intents. If an activity has no intent filter, or it doesn’t include a category name of android.intent.category.DEFAULT, it means that the activity can’t be started with an implicit intent. It can only be started with an explicit intent using the full name.

Leave a Reply

Your email address will not be published. Required fields are marked *