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

Activities and Intents: Lesson 2

This document provides an overview of implicit intents in Android. It defines what an implicit intent is, how the Android system handles implicit intents, and how to send an implicit intent from one app to another app. It also covers how to specify actions, data, extras, MIME types, and categories when sending an implicit intent. The document contains code examples for common tasks like making a phone call, opening a web page, and performing a web search via an implicit intent.
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)
50 views

Activities and Intents: Lesson 2

This document provides an overview of implicit intents in Android. It defines what an implicit intent is, how the Android system handles implicit intents, and how to send an implicit intent from one app to another app. It also covers how to specify actions, data, extras, MIME types, and categories when sending an implicit intent. The document contains code examples for common tasks like making a phone call, opening a web page, and performing a web search via an implicit intent.
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/ 35

Android Developer Fundamentals V2

Activities and
Intents
Lesson 2

This work is licensed under a


Android
Android Developer
DeveloperFundamentals
Fundamentals V2
V2 Implicit Intents Creative Commons Attribution 4.0 Inter 1
national License
2.3 Implicit Intents

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 2
national License
Contents

● Intent—recap
● Implicit Intent overview
● Sending an implicit Intent
● Receiving an implicit Intent

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 3
national License
Recap: Intent

Android Developer Fundamentals V2 4


What is an Intent?
An Intent is:
● Description of an operation to be performed
● Messaging object used to request an action from another
app component via the Android system.
Originator App component

Intent Action
Android
System

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 5
national License
What can an Intent do?
An Intent can be used to:
● start an Activity
● start a Service
● deliver a Broadcast

Services and Broadcasts are covered in other lessons

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 6
national License
Explicit vs. implicit Intent

Explicit Intent — Starts an Activity of a specific class

Implicit Intent — Asks system to find an Activity class with a


registered handler that can handle this request

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 7
national License
Implicit Intent
overview

Android Developer Fundamentals V2 8


What you do with an implicit Intent
● Start an Activity in another app by describing an action
you intend to perform, such as "share an article", "view a
map", or "take a picture"
● Specify an action and optionally provide data with which
to perform the action
● Don't specify the target Activity class, just the intended
action
This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 9
national License
What system does with implicit Intent
● Android runtime matches the implicit intent request with
registered intent handlers
● If there are multiple matches, an App Chooser will open to
let the user decide

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 10
national License
How does implicit Intent work?
1. The Android Runtime keeps a list of registered Apps
2. Apps have to register via AndroidManifest.xml
3. Runtime receives the request and looks for matches
4. Android runtime uses Intent filters for matching
5. If more than one match, shows a list of possible matches
and lets the user choose one
6. Android runtime starts the requested activity

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 11
national License
App Chooser

When the Android runtime finds


multiple registered activities that can
handle an implicit Intent, it displays an
App Chooser to allow the user to
select the handler

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 12
national License
Sending an
implicit Intent

Android Developer Fundamentals V2 13


Sending an implicit Intent
1. Create an Intent for an action
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);

User has pressed Call button — start Activity that can make
a call (no data is passed in or returned)

2. Start the Activity


if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 14
national License
Avoid exceptions and crashes
Before starting an implicit Activity, use the package manager
to check that there is a package with an Activity that matches
the given criteria.

Intent myIntent = new Intent(Intent.ACTION_CALL_BUTTON);

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 15
national License
Sending an implicit Intent with data URI
1. Create an Intent for action
Intent intent = new Intent(Intent.ACTION_DIAL);

2. Provide data as a URI


intent.setData(Uri.parse("tel:8005551234"));

3. Start the Activity


if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 16
national License
Providing the data as URI

Create an URI from a string using Uri.parse(String uri)

● Uri.parse("tel:8005551234")
● Uri.parse("geo:0,0?q=brooklyn%20bridge%2C%20brooklyn%2C%20ny")
● Uri.parse("http://www.android.com");

Uri documentation

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 17
national License
Implicit Intent examples
Show a web page
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
Dial a phone number
Uri uri = Uri.parse("tel:8005551234");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 18
national License
Sending an implicit Intent with extras
1. Create an Intent for an action
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);

2. Put extras
String query = edittext.getText().toString();
intent.putExtra(SearchManager.QUERY, query));

3. Start the Activity


if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 19
national License
Category
Additional information about the kind of component to
handle the intent.
● CATEGORY_OPENABLE
Only allow URIs of files that are openable
● CATEGORY_BROWSABLE
Only an Activity that can start a web browser to display
data referenced by the URI
This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 20
national License
Sending an implicit Intent with type and category

1. Create an Intent for an action


Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

2. Set mime type and category for additional information


intent.setType("application/pdf"); // set MIME type
intent.addCategory(Intent.CATEGORY_OPENABLE);

continued on next slide...

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 21
national License
Sending an implicit Intent with type and category
3. Start the Activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(myIntent,ACTIVITY_REQUEST_CREATE_FILE);
}

4. Process returned content URI in onActivityResult()

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 22
national License
Common actions for an implicit Intent
Common actions include:
● ACTION_SET_ALARM
● ACTION_IMAGE_CAPTURE
● ACTION_CREATE_DOCUMENT
● ACTION_SENDTO
● and many more
This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 23
national License
Apps that handle common actions
Common actions are usually handled by installed apps (both
system apps and other apps), such as:

● Alarm Clock, Calendar, Camera, Contacts ➔ List of common


● Email, File Storage, Maps, Music/Video actions for an im
plicit intent
● Notes, Phone, Search, Settings
➔ List of all availab
● Text Messaging and Web Browsing le actions

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 24
national License
Receiving an
Implicit Intent

Android Developer Fundamentals V2 25


Register your app to receive an Intent
● Declare one or more Intent filters for the Activity in
AndroidManifest.xml
● Filter announces ability of Activity to accept an implicit
Intent
● Filter puts conditions on the Intent that the Activity
accepts

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 26
national License
Intent filter in AndroidManifest.xml
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 27
national License
Intent filters: action and category

● action — Match one or more action constants


○ android.intent.action.VIEW — matches any Intent with ACTION_VIEW
○ android.intent.action.SEND — matches any Intent with ACTION_SEND

● category — additional information (list of categories)


○ android.intent.category.BROWSABLE—can be started by web browser
○ android.intent.category.LAUNCHER—Show activity as launcher icon

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 28
national License
Intent filters: data

● data — Filter on data URIs, MIME type


○ android:scheme="https"—require URIs to be https protocol
○ android:host="developer.android.com"—only accept an Intent from
specified hosts
○ android:mimeType="text/plain"—limit the acceptable types of documents

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 29
national License
An Activity can have multiple filters
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
...
</intent-filter>
<intent-filter>
<action
android:name="android.intent.action.SEND_MULTIPLE"/>
...
</intent-filter> An Activity can have several filters
</activity> Android Developer Fundamentals V2 Implicit Intents
This work is licensed under a
Creative Commons Attribution 4.0 Inter 30
national License
A filter can have multiple actions & data
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
</intent-filter>

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 31
national License
Learn more

Android Developer Fundamentals V2 32


Learn more
● Intent class documentation
● Uri documentation
● List of common apps that respond to implicit intents
● List of available actions
● List of categories
● Intent Filters
This work is licensed under a
Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 33
national License
What's Next?

● Concept Chapter: 2.3 Implicit Intents


● Practical: 2.3 Implicit Intents

This work is licensed under a


Android Developer Fundamentals V2 Implicit Intents Creative Commons Attribution 4.0 Inter 34
national License
END

Android Developer Fundamentals V2 35

You might also like