26

Android Bootcamp Tanzania:intents

Embed Size (px)

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

Page 1: Android Bootcamp Tanzania:intents
Page 2: Android Bootcamp Tanzania:intents

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.

Page 3: Android Bootcamp Tanzania:intents

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.

Page 4: Android Bootcamp Tanzania:intents

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.

Page 5: Android Bootcamp Tanzania:intents

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.

Page 6: Android Bootcamp Tanzania:intents

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

Page 7: Android Bootcamp Tanzania:intents

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?

Page 8: Android Bootcamp Tanzania:intents
Page 9: Android Bootcamp Tanzania:intents
Page 10: Android Bootcamp Tanzania:intents

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

Page 11: Android Bootcamp Tanzania:intents

Example on Explicit Intents

Page 12: Android Bootcamp Tanzania: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);

Page 13: Android Bootcamp Tanzania:intents

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.

Page 14: Android Bootcamp Tanzania:intents

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

Page 15: Android Bootcamp Tanzania:intents
Page 16: Android Bootcamp Tanzania:intents

Labs – Joining Activities

Page 17: Android Bootcamp Tanzania:intents

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

Page 18: Android Bootcamp Tanzania:intents
Page 19: Android Bootcamp Tanzania:intents

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.

Page 20: Android Bootcamp Tanzania:intents
Page 21: Android Bootcamp Tanzania:intents

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.

Page 22: Android Bootcamp Tanzania:intents

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()

Page 23: Android Bootcamp Tanzania:intents

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

Page 24: Android Bootcamp Tanzania:intents

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().

Page 25: Android Bootcamp Tanzania:intents
Page 26: Android Bootcamp Tanzania:intents