0% found this document useful (0 votes)
53 views31 pages

Lecture 5 - MAD

The document discusses intents and activities in Android application development. It defines intents as facilities for late runtime binding that allow components to call each other and pass data. Explicit intents specify a component by name, while implicit intents rely on data types and names for the system to choose a matching component. The document also covers declaring activities in the manifest, passing data between activities via intents, and returning data from launched activities using startActivityForResult and onActivityResult.

Uploaded by

Danial Aqil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views31 pages

Lecture 5 - MAD

The document discusses intents and activities in Android application development. It defines intents as facilities for late runtime binding that allow components to call each other and pass data. Explicit intents specify a component by name, while implicit intents rely on data types and names for the system to choose a matching component. The document also covers declaring activities in the manifest, passing data between activities via intents, and returning data from launched activities using startActivityForResult and onActivityResult.

Uploaded by

Danial Aqil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

BCI3283-Mobile Application Development

Intent

By
Ts. Dr. Nabilah Radzuan

Faculty of Computing
Intent

• Aims
To learn how to passing and receive data between different activities.

• Expected Outcomes
– Ability to handle explicit Intent
– Ability to handle implicit Intent
– Passing data to other activity
– Returning data from launched activity

• References
– http://codetheory.in/
– http://www.techotopia.com/
Activities and AndroidManifest.xml

 An Android application can be composed of multiple


Activities …
 Each activity should be declared in the file:
AndroidManifest.xml
 Add a child element to the <application> tag:
<application>
<activity android:name=".MyActivity" />
<activity android:name=”.SecondActivity" />
</application>

3
Activities and AndroidManifest.xml

 Each activity has its


 Java class and
 layout file.
public class FirstActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}

public class SecondActivity extends Activity {


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
}

4
Intent
In order to launch another like B Activity from A Activity,
programmer have to use Intent.

 Activity A fires an Intent

 Android looks for capable


activities
 By looking at the manifests

 When one is found, it is


called

5
Intent Definition

Intent: facility for late run-time binding between


components in the same or different applications.

 Call a component from another component


 Possible to pass data between components
 Components: Activities, Services, Broadcast receivers …
 Something like:
 “Android, please do that with this data”
 Reuse already installed applications and components

6
Intent Definition

 We can think to an “Intent” object as a message


containing a bundle of information.
 Information of interests for the receiver (e.g. name)
 Information of interests for the Android system (e.g. category).

Component Name
Action Name

Structure Data
of an Intent Category
Extra Flags

7
Intent types

INTENT TYPES

EXPLICIT IMPLICIT

The target receiver is specified The target receiver is specified


through the Component Name by data type/names.

Used to launch specific Activities The system chooses the receiver


that matches the request.
Used to launch specific Apps

8
Explicit Intent:

• Explicit Intent: Lunching


an activity and passing
information from one to
another in the same
application.
Implicit Intent:
• Implicit Intent: In this case programmer can avoid coding
his own activity and let Android automatically handle the
activity by firing up the component from other
applications that can do the job on our app’s behalf.
Intents Capability

Programmer will be able to accomplish a lot of


things using intent :
1. The ability of navigating from one activity to
another Explicit intent

2. The ability to start a third party application


(external app’s activity) like using a camera,
contact, pinning location, sending email and
etc.. Implicit intent
Intent types: Explicit Intents

 Explicit Intent: Specify the name of the Activity that will


handle the intent.

Intent intent=new Intent(MainActivity.this, SecondActivity.class);


startActivity(intent);

•Lunching the Second Activity.java from the MainActivity.java.,


• For that purpose we use the Intent class and passing the current context (intent)
by calling startActivity().  
•This will simply launch the Second Activity from the same application right over
MainActivity.

1
Passing Data to other Activity

• Add data to intent in key-value pairs then


retrieve it by the receiving activity.
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name","Ali")
intent.putExtra("age",21)
startActivity(intent)
String

Only a name Integer

In this case, two pieces of data (String and integer) have


been transferred by using putExtra.
Receiving Data in Other Activity
• To receive the data in B Activity by using Bundle object.
// We are currently in SecondActivity.java
Bundle extras =getIntent().getExtras();
if(extras !=null){ Only a
String name = extras.getString("name"); name
Int age = extras.getInt("age")
}
• Calling getExtras() on our Intent object enables us to call various
methods like getString(), getInt(),getChar(), etc.
• also we can use methods like getStringExtra() and getIntExtra()
on the Intent object instead of working with bundles .
// We are currently in SecondActivity.java
String name = getIntent().getStringExtra("name");
Int age = getIntent().getIntExtra("age")
Returning Data Launched Activity

• There is no way for transferred data to be returned from


the Second Activity to the first activity.
• However, launching the Second Activity as a sub-activity
of the first Activity can achieved returning the data.
• To do so, we have to start the first activity as a sub-
activity by calling startActivityForResult() method
instead of using startActivity().
• This method enable us to identify the returned data
from the sub-activity by passing a REQUEST_CODE value.
startActivityForResult(intent, CHOOSE_ACTIVITY_CODE);
Returning Data Launched Activity

 Activities can return results (e.g. data)


 Sender side: invoke the startActivityForResult()
 onActivityResult(int requestCode, int resultCode, Intent data)
 startActivityForResult(Intent intent, int requestCode);

Intent intent = new Intent(MainActivity.this, SecondActivity.class);


intent.putExtra("name","ali");
Intent.putExtra("age",21);
startActivityForResult(intent, CHOOSE_ACTIVITY_CODE);

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
// Invoked when SecondActivity completes its operations …
}

1
Intent with Results

 Activities can return results (e.g. data)


 Receiver side: invoke the setResult()
 void setResult(int resultCode, Intent data)

Intent intent=new Intent();


setResult(RESULT_OK, intent);
intent.putExtra("result", "coming from 2nd activity" );
finish();

 The result is delivered to the caller component only after


invoking the finish() method!

1
}

Intent intent = new Intent(MainActivity.this, SecondActivity.class);


intent.putExtra("name","ali");
Intent.putExtra("age",21);
startActivityForResult(intent, CHOOSE_ACTIVITY_CODE);

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode==22 && resultCode==RESULT_OK)
{
textview.setText(data.getStringExtra("result"));
}
CHOOSE_ACTIVITY
_CODE
Returning Data from Launched Activity

• Returning data to the first activity:


– Calling the finish() method in the sub-activity.
finish() method creates a new intent object contains the
returned data.
– Calling the setResult() method of the enclosing activity to pass
through it the return data.
• The result code is typically
– RESULT_OK: the result sent correctly
– RESULT_CANCELED: will be received in the parent activity if
the event of sub-activity is crashed
– May also be a custom value subject.
Java Code Example

Main Activity
Capturing information From the EditText, send it to the second
activity, then do listening
Java Code Example

Second Activity
Receiving the information and display it into TextView
Java Code Example

Second Activity
Capturing information From the EditText and send it to the main
activity (parent Activity)
Intent types

INTENT TYPES

EXPLICIT IMPLICIT

The target receiver is specified The target receiver is specified


through the Component Name by data type/names.

Used to launch specific Activities The system chooses the


that matches the request.
receiver

2
Intent types: Implicit Intents

 Implicit Intents: do not name a target (component


name is left blank) …
 When an Intent is launched, Android checks out
which activies might answer to the Intent …
 If at least one is found, then that activity is started!

 Binding (connections) does not occur at compile time, nor


at install time, but at run-time …(late run-time binding)
Intent Components

 We can think to an “Intent” object as a message


containing a bundle of information.
 Information of interests for the receiver (e.g. data)
 Information of interests for the Android system (e.g. category).

Component Name A string naming the action to


Action Name be performed.

Data Pre-defined, or can be


Category specified by the programmer.
Extra Flags
void setAction(String)

2
Intent Components

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

Action Name Description


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


 it.example.projectpackage.FILL_DATA (package prefix + name action)

2
Intent Components

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


Action Name Description
ACTION_IMAGE_CAPTION Open the camera and receive a photo
ACTION_VIDEO_CAPTION Open the camera and receive a video

ACTION_DIAL Open the phone app and dial a phone number


ACTION_SENDTO Send an email (email data contained in the
extra)
ACTION_SETTINGS Open the system setting
ACTION_WIRELESS_SETTINGS Open the system setting of the
wireless interfaces
ACTION_DISPLAY_SETTINGS Open the system setting of the display

2
Actions
Intent intent=new
Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.ump.edu.my”));
startActivity(intent);

Intent intent=new
Intent(Intent.ACTION_VIEW,
Uri.parse("sms:006011111111"));
startActivity(intent);

Intent intent=new
Intent(Intent.ACTION_DIAL,
Uri.parse("tel:006011111111"));
startActivity(intent);
Intent Components

 Example of Implicit Intent that initiates a web search.


public void doSearch(String query) {
Intent intent =new Intent(Intent.ACTION_SEARCH);
Intent.putExtra(SearchManager.QUERY,query);
if (intent.resolveActivity(getPackageManager()) !=null)
startActivity(intent)
}

 Example of Implicit Intent that plays a music file.


public void playMedia(Uri file) {
Intent intent =new Intent(Intent.ACTION_VIEW);
if (intent.resolveActivity(getPackageManager()) !=null)
startActivity(intent)
}

2
Intent types: Implicit Intents
Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("www.ump.edu.my"));
startActivity(i);

Action to perform Data to


perform the action on
 Implicit intents are very useful to re-use code and to launch
external applications …
 Android system will search for activities on the device that have
registered the ability to handle ACTION_VIEW requests.

3
Common Intents

 Setting an Alarm – ACTION_SET_ALARM


 Timer – ACTION_SET_TIMER
 Calendar Events – ACTION_INSERT and data
 Camera – ACTION_IMAGE_CAPTURE /
ACTION_VIDEO_CAPTURE
 Email – ACTION_SEND /
ACTION_SENDTO
 Files – ACTION_GET_CONTENT
 Various meanings – ACTION_VIEW
More at: https://developer.android.com/guide/components/intents-common.html

You might also like