Android Bootcamp Tanzania:intents

Preview:

DESCRIPTION

The Content helps those who wish to program mobile applications using android platform. The content has been used to conduct mobile application boot camps using android platform on different regions in Tanzania

Citation preview

An intent is an abstract description of an operation to be performed.

It can be used with startActivity to launch an Activity.

An Intent provides a facility for performing late runtime binding between the code in different applications.

Its most significant use is in the launching of activities, where it can be thought of as the glue between activities.

It is basically a passive data structure holding an abstract description of an action to be performed.

The primary pieces of information in an intent are:◦ action -- The general action to be performed, such

as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

◦ data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.

There are two primary forms of intents you will use.

Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)).

Which provides the exact class to be run.

Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.

Implicit Intents have not specified a component;

Instead, they must include enough information for the system to determine which of the available components is best to run for that intent.

Predefined actions (http://developer.android.com/reference/android/content/Intent.html)

6

Action Name Description

ACTION_CALL Initiate a phone call

ACTION_EDIT Display data to edit

ACTION_MAIN Start as a main entry point, does not expect to receive data.

ACTION_PICK Pick an item from the data, returning what was selected.

ACTION_VIEW Display the data to the user

ACTION_SEARCH Perform a search

Defined by the programmer

Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));

startActivity(i);

7

Action to perform Data to perform the action on

Implicit intents are very useful to re-use code and to launch external applications …

More than a Component can match the Intent request …

How to define the target component?

In order to access external components in android, permissions needs to be set in Android Manifest file.

Example on Explicit Intents

12

Explicit Intent: Specify the activity that will handle the intent.

The following shows how to move from First activity to second activity.

Intent a =new Intent(FirstActivity.this, SecondActivity.class);startActivity(a);ORIntent about=new Intent(getApplicationContext(),About.class);startActivity(about);

Developers mostly create applications using different activities.

These activities needs to communicate and pass data from one part to the other.

For this case, Explicit Intents should be used.

For activities to be able to communicate to one another.

They must be registered in the manifest file.

This is done by adding all your activities in the manifest file.

This is done inside the <application> tag of the manifest

Labs – Joining Activities

Creating an Options Menu The options menu is where you should

include actions and other options that are relevant to the current activity context.

◦ Such as "Search“,”Save” "Compose email," and "Settings.“

◦ Ref: http://www.droidnova.com/how-to-create-an-option-menu,427.htm

l

First we have to make a new folder in our res/layout directory .

In this new directory we will create a new xml file named menu.xml.

Put the following code.

The content of the xml file should be very self explaining.

We have an id for each item, so we have a reference for it.

The title attribute, if defined, is nothing more than the text you see in the option menu.

The same with the icon attribute which references to an icon, in our case the default icon.

Now we have to modify our Activity class.

First we have to override the method onCreateOptionsMenu()

 menu inflator loads the menu.xml file to set the menu

Right now we can start our application and we will see our 3 option items in our menu when we press the menu button.

As long as nothing happens when we press one item, the menu is senseless.

To implement a reaction of our menu, we should override another method named onOptionsItemSelected().