0% found this document useful (0 votes)
2 views

FU Lecture 05 06

Uploaded by

Chị Anh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FU Lecture 05 06

Uploaded by

Chị Anh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Linking Activities

Using Intents

Linking Activities
Today's Agenda
 Invoking Activities by class name

o Defining dimensions in res/values


o Sending data via the “extras” Bundle

 Invoking Activities with a URI

o Sending data via parameters in the URI

 Invoking Activities with tabbed windows

o Defining two-image icons in res/drawable

 Calling Built-In Applications Using Intents


Android Intents
 An Android Intent is an object carrying an intent

 Example: message from one component to another component with-in


the application or outside the application.
 The intents can communicate messages among any of the three core
components of an application - activities, services, and broadcast receivers

“The intent itself, an Intent object, is a passive data structure holding an


abstract description of an operation to be performed”
Android Intents
 The intent itself, an Intent object, is a passive data structure holding an
abstract description of an operation to be performed.
 Context.startActivity() : The Intent object is passed to this method to
launch a new activity or get an existing activity to do something new.
 Context.startService() : The Intent object is passed to this method to
initiate a service or deliver new instructions to an ongoing service.
 Context.sendBroadcast() :The Intent object is passed to this method to
deliver the message to all interested broadcast receivers.
Android Intents Objects
 An Intent object is a bundle of information which is used by the component
that receives the intent plus information used by the Android system.
 An Intent object can contain the following components based on what it is
communicating or going to perform:
 ACTION

 DATA

 CATEGORY

 EXTRAS

 FLAGS

 COMPONENT NAME
Types of Intents
 EXPLICIT INTENTS

These intents designate the target component by its name and they are
typically used for application-internal messages - such as an activity
starting a subordinate service or launching a sister activity.
Intent i = new Intent(this, TargetActivity.class);

 IMPLICIT INTENTS

These intents do not name a target and the field for the component name
is left blank. Implicit intents are often used to activate components in
other applications.
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));
Invoking Activities by class name
 Exactly one Activity can match

 New Activity must be in same project as original

 Can send data via an “extras” Bundle


Intent i = new Intent(this, TargetActivity.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
startActivity(i);
Invoking Activities by class name
 Java (original Activity)
Intent activityIntent = new Intent(this, NewActivity.class);
startActivity(activityIntent);
 XML (AndroidManifest.xml)
<activity android:name=".NewActivity"
android:label="@string/some_app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Sending Data via the “Extras” Bundle
 Attach a Bundle (like a Map – see next slides) to the Intent. The
Bundle will contain data to be used by the new Activity
 Syntax
Intent activityIntent = new Intent(this, NewActivity.class);
Bundle newActivityInfo = new Bundle();
newActivityInfo.putBlah(key, value); // putDouble, putString, etc.
activityIntent.putExtras(newActivityInfo);
startActivity(activityIntent);

The keys must be Strings.


The values must be of the standard types
Invoking Activities with a URI
 More than one Activity could match

 New Activity need not be in the same project as original

 Can send data via URI parameters or “extras” Bundle

 Supply a URI that indirectly refers to new Activity. The new Activity
registers as target for URIs of a certain form.
Invoking Activities with a URI
 Java (original Activity)
Uri uri = Uri.parse("foo://bar.example.com/baz");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent );

 XML (AndroidManifest.xml)
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="foo" android:host="bar.example.com" />
</intent-filter>
Invoking Activities with a URI
 Matching the URI itself
o Register for a scheme and a host
 Example URI: foo://bar.example.com/baz
 intent-filter entry
• <data android:scheme=“foo" android:host="bar.example.com" />
• Note that the “baz” part is arbitrary – just to make URL look better.
 Matching the data type
o Register for a MIME type
 Example URIs
• content:// (referring to that MIME type)
• file:// (referring to that MIME type)
• anything:// (the Intent can call setType to specify MIME type)
 intent-filter entry
• <data android:mimeType="some/type" />
• <data android:mimeType="something/*" />
Predefined Action/URI Combinations
Action URI Meaning

Intent.ACTION_CALL tel:phone_number Opens phone application and calls


phone_number.
Intent.ACTION_DIAL tel:phone_number Opens phone application and dials (but
doesn’t call) phone_number.
Intent.ACTION_DIAL voicemail: Opens phone application and dials (but
doesn’t call) the voice mail number.

Intent.ACTION_VIEW geo:lat,long Opens the maps application centered on


(lat, long).
Intent.ACTION_VIEW geo:0,0?q=address Opens the maps application centered on
the specified address.
Intent.ACTION_VIEW http://url Opens the browser application to the
https://url specified address.

Intent. plain_text Opens the browser application and uses


ACTION_WEB_SEARCH Google search for given string.
Sending Data via Parameters in the URI
 Java (original Activity)
String address =
"loan://coreservlets.com/calc?loanAmount=xxx&…";
Uri uri = Uri.parse(address);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(activityIntent);
 Java (new Activity)
Uri uri = getIntent().getData();
String loanAmountString = uri.getQueryParameter("loanAmount");
// Convert String to number if needed
Invoking Activities with tabbed windows
 Make tabbed windows. Each tab invokes a different Activity, or
an Activity with different data.
 Can use class name or URI to specify Activity

 New Activity must be in same project as original

 Can send data via URI parameters or “extras” Bundle


Extends TabActivity. Uses TabHost and TabSpec
Invoking Activities with tabbed windows
public class SomeActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources resources = getResources();
TabHost host = getTabHost();
Intent intent1= ...;
Drawable tabIcon = resources.getDrawable(R.drawable.icon_name);
TabSpec tab1Spec = host.newTabSpec("Tab One")
.setIndicator("Some Text", tabIcon)
.setContent(intent1);
host.addTab(tab1Spec);
// Repeat for other tabs
}}
Defining Tab Icons
 Idea
o Although it is legal to call setIndicator(someString), the resultant tab
looks bad because of blank space at top. So, more common to do
setIndicator(someString, someIcon).
 You can also do setIndicator(someView) for fancy tabs
 Icon option 1
o Use a single image for the icon
 Same image used when the tab is or is not selected
 Icon option 2
o Use 2 similar but differently colored images for the icon
 One for when selected, one for when not
Option 1: A Single Image
 Put image file in res/drawable/some_icon.png
 Refer to image with
 Drawable tabIcon =
resources.getDrawable(R.drawable.some_icon);
 Put icon in tab label with
 tabSpec.setIndicator("Some Text", tabIcon);
Option 2: Two Images (Normal and
Selected)
 Put image files in
 res/drawable/some_icon_normal.png and
 res/drawable/some_icon_selected.png
 Make XML file (next page)
 res/drawable/some_icon.xml
 Refer to XML file with
 Drawable tabIcon =
resources.getDrawable(R.drawable.some_icon);
 Put icon in tab label with
 tabSpec.setIndicator("Some Text", tabIcon);
XML Code for Dual-Image Icon
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://...">
<!-- When tab selected, use some_icon_selected.png -->
<item android:drawable="@drawable/some_icon_selected"
android:state_selected="true" />
<!-- When tab not selected, use some_icon_normal.png -->
<item android:drawable="@drawable/some_icon_normal" />
</selector>
Calling Built-In Applications Using Intents
 Using the intent to

 call activities within your own application

 call activities from other applications

 In particular, your application can call the many built-in


applications that are included with an Android device. You can
simply use an Intent object to bring up
 How to call some of the built-in applications commonly
found on an Android device?
How It Works
 In Android, intents usually come in pairs: action and data.
 The action describes what is to be performed, such as editing an
item, viewing the content of an item, and so on.

 The data specifies what is affected, such as a person in the Contacts


database. The data is specified as an Uri object.

 Some examples of action are as follows:


 ACTION_VIEW

 ACTION_DIAL

 ACTION_PICK
How It Works
 Some examples of data include the following:

 http://www.google.com

 tel:+651234567

 geo:37.827500,-122.481670

 content://contacts

 Collectively, the action and data pair describes the operation to


be performed.
How It Works
 You create an Intent object and then pass two arguments to its
constructor, the action and the data:
Intent i = new Intent( android.content.Intent.ACTION_VIEW,
Uri. parse(“http://www.amazon.com”));
startActivity(i);

 The android.content.Intent.ACTION_VIEW constant actually


refers to the “android.intent.action.View ” action, so the preceding
could be rewritten as follows:
Intent i = new Intent( android.content.Intent.VIEW,
Uri. parse(“http://www.amazon.com”));
startActivity(i);
Example
 Make calls
Intent i = new Intent( android.content.Intent.ACTION_DIAL,
Uri.parse(“tel:+651234567”));
startActivity(i);

 Show Map

Intent i = new Intent(android.content.Intent.ACTION_VIEW,


Uri.parse(“geo:37.827500,-122.481670”));
startActivity(i);

You might also like