Android Developer Fundamentals V 2 Activities and Intents

  • Slides: 38
Download presentation
Android Developer Fundamentals V 2 Activities and Intents Lesson 2 Android. Developer. Fundamentals. V

Android Developer Fundamentals V 2 Activities and Intents Lesson 2 Android. Developer. Fundamentals. V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 1

2. 3 Implicit Intents Android Developer Fundamentals V 2 Implicit Intents This work is

2. 3 Implicit Intents Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 2

Recap: Intent Android Developer Fundamentals V 2 3

Recap: Intent Android Developer Fundamentals V 2 3

What is an Intent? An Intent is: ● Description of an operation to be

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 Intent App component Action Android System Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 4

What can an Intent do? An Intent can be used to: ● start an

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 Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 5

Explicit vs. implicit Intent Explicit Intent — Starts an Activity of a specific class

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 Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 6

Implicit Intent overview Android Developer Fundamentals V 2 7

Implicit Intent overview Android Developer Fundamentals V 2 7

What you do with an implicit Intent ● Start an Activity in another app

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 Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 8

What you do with an implicit Intent Android Developer Fundamentals V 2 Implicit Intents

What you do with an implicit Intent Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 9

Apps that handle common actions Common actions are usually handled by installed apps (both

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 ● Email, File Storage, Maps, Music/Video ● Notes, Phone, Search, Settings ● Text Messaging and Web Browsing Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 10

How does implicit Intent work? 1. 2. 3. 4. 5. The Android Runtime keeps

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

App Chooser When the Android runtime finds multiple registered activities that can handle an

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 Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 12

Sending an implicit Intent Android Developer Fundamentals V 2 13

Sending an implicit Intent Android Developer Fundamentals V 2 13

Sending an implicit Intent 1. Create an Intent for an action Intent intent =

Sending an implicit Intent 1. Create an Intent for an action Intent intent = new Intent(Intent. ACTION_DIAL ); User has pressed Call button — start Activity that can make a call (no data is passed in or returned) 1. Start the Activity if (intent. resolve. Activity(get. Package. Manager()) != null) { start. Activity(intent); } Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 14

Avoid exceptions and crashes Before starting an implicit Activity, use the package manager to

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 my. Intent = new Intent(Intent. ACTION_DIAL); if (intent. resolve. Activity(get. Package. Manager()) != null) { start. Activity(intent); } Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 15

Sending an implicit Intent with data URI 1. Create an Intent for action Intent

Sending an implicit Intent with data URI 1. Create an Intent for action Intent intent = new Intent(Intent. ACTION_DIAL ); 1. Provide data as a URI intent. set. Data(Uri. parse("tel: 8005551234") ); 1. Start the Activity if (intent. resolve. Activity(get. Package. Manager()) != null) { start. Activity(intent); } Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 16

Providing the data as URI Create an URI from a string using Uri. parse(String

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%20 bridge%2 C%20 brooklyn%2 C%20 ny") ● Uri. parse("http: //www. android. com"); Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 17

Implicit Intent examples Show a web page Uri uri = Uri. parse("http: //www. google.

Implicit Intent examples Show a web page Uri uri = Uri. parse("http: //www. google. com"); Intent it = new Intent(Intent. ACTION_VIEW, uri); start. Activity(it); Dial a phone number Uri uri = Uri. parse("tel: 8005551234"); Intent it = new Intent(Intent. ACTION_DIAL, uri); start. Activity(it); Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 18

Sending an implicit Intent with extras 1. Create an Intent for an action Intent

Sending an implicit Intent with extras 1. Create an Intent for an action Intent intent = new Intent(Intent. ACTION_WEB_SEARCH ); 1. Put extras String query = edittext. get. Text(). to. String(); intent. put. Extra(Search. Manager. QUERY, query)); 1. Start the Activity if (intent. resolve. Activity(get. Package. Manager()) != null) { start. Activity(intent); } Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 19

Category Additional information about the kind of component to handle the intent. ● CATEGORY_OPENABLE

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 Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 20

Sending an implicit Intent with type and category 1. Create an Intent for an

Sending an implicit Intent with type and category 1. Create an Intent for an action Intent intent = new Intent(Intent. ACTION_CREATE_DOCUMENT ); 1. Set mime type and category for additional information intent. set. Type("application/pdf"); // set MIME type intent. add. Category(Intent. CATEGORY_OPENABLE); continued on next slide. . . Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 21

Sending an implicit Intent with type and category 3. Start the Activity if (intent.

Sending an implicit Intent with type and category 3. Start the Activity if (intent. resolve. Activity(get. Package. Manager()) != null) { start. Activity. For. Result(my. Intent, ACTIVITY_REQUEST_CREATE_FILE); } 4. Process returned content URI in on. Activity. Result() Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 22

Common actions for an implicit Intent Common actions include: ● ACTION_SET_ALARM ● ACTION_IMAGE_CAPTURE ●

Common actions for an implicit Intent Common actions include: ● ACTION_SET_ALARM ● ACTION_IMAGE_CAPTURE ● ACTION_CREATE_DOCUMENT ● ACTION_SENDTO ● and many more Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 23

Show the app chooser In many cases the user has a preferred app for

Show the app chooser In many cases the user has a preferred app for a given task. However, you can choose to explicitly show a chooser dialog every time. Intent send. Intent = new Intent(Intent. ACTION_SEND); String title = get. Resources(). get. String(R. string. chooser_title); Intent chooser = Intent. create. Chooser(send. Intent, title); if (send. Intent. resolve. Activity(get. Package. Manager()) != null) { start. Activity(chooser); } Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 24

Receiving an Implicit Intent Android Developer Fundamentals V 2 25

Receiving an Implicit Intent Android Developer Fundamentals V 2 25

Register your app to receive an Intent ● Declare one or more Intent filters

Register your app to receive an Intent ● Declare one or more Intent filters for the Activity in Android. Manifest. xml ● Filter announces ability of Activity to accept an implicit Intent ● Filter puts conditions on the Intent that the Activity accepts Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 26

Intent filter in Android. Manifest. xml <activity android: name="Share. Activity "> <intent-filter> <action android:

Intent filter in Android. Manifest. xml <activity android: name="Share. Activity "> <intent-filter> <action android: name="android. intent. action. SEND"/> <category android: name="android. intent. category. DEFAULT"/> <data android: mime. Type="text/plain"/> </intent-filter> </activity> Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 27

Intent filters: action and category ● action — Match one or more action constants

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 ○ android. intent. category. BROWSABLE—can be started by web browser ○ android. intent. category. LAUNCHER—Show activity as launcher icon Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 28

Intent filters: data ● data — Filter on data URIs, MIME type ○ android:

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: mime. Type="text/plain"—limit the acceptable types of documents <intent-filter> <data android: mime. Type="video/mpeg" android: scheme="https" /> <data android: mime. Type="audio/mpeg" android: scheme="http" /> </intent-filter> Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 29

An Activity can have multiple filters <activity android: name="Share. Activity"> <intent-filter> <action android: name="android.

An Activity can have multiple filters <activity android: name="Share. Activity"> <intent-filter> <action android: name="android. intent. action. SEND"/>. . . </intent-filter> <action android: name="android. intent. action. SEND_MULTIPLE"/>. . . </intent-filter> </activity> Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 30

A filter can have multiple actions & data <intent-filter> <action android: name="android. intent. action.

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: mime. Type="image/*"/> <data android: mime. Type="video/*"/> </intent-filter> Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 31

Managing tasks Say you have an app with three Activity objects: A, B, and

Managing tasks Say you have an app with three Activity objects: A, B, and C. A launches B with an Intent, and B launches C. C, in turn sends an Intent to launch A. In this case the system creates a second instance of A on the top of the stack. Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 32

Managing tasks Say your Activity C can be launched from a second app with

Managing tasks Say your Activity C can be launched from a second app with an implicit Intent. stack for that second app's task. The user runs the second app, which has its own task and its own back stack. If that app launch your Activity C, a new instance of C is created and placed on the back Stack of task 2 Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 33

Activity launch modes To define a launch mode for an Activity add the android:

Activity launch modes To define a launch mode for an Activity add the android: launch. Mode attribute to the <activity> element in the Android. Manifest. xml <activity android: name=". Second. Activity" android: label="@string/activity 2_name" android: parent. Activity. Name=". Main. Activity" android: launch. Mode="standard"> <!-- More attributes. . . --> </activity> Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 34

Activity launch modes ● standard ● single. Top: If an instance of an Activity

Activity launch modes ● standard ● single. Top: If an instance of an Activity exists at the top of the back stack of current task, Android routes new Intent to the existing Activity instance rather than creating a new instance. A new Activity is still instantiated if there is an existing Activity anywhere in the back stack other than the top. ● single. Task: When the Activity is launched the system creates a new task for that Activity. If another task already exists with an instance of that Activity, the system routes the Intent to that Activity instead. ● single. Instance: Same as single task, except that the system doesn't launch any other Activity into the task holding the Activity instance. Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 35

Intent flags ● Intent flags are options that specify how the activity that receives

Intent flags ● Intent flags are options that specify how the activity that receives the intent should handle that intent. ● You add Intent flags to an Intent object with set. Flag() or add. Flag(). ● Intent flags always take precedence over the launch mode in case of conflicts. Android Developer Fundamentals V 2 Implicit Intents This work is licensed under a Creative Commons Attribution 4. 0 International License. 36

Intent flags ● FLAG_ACTIVITY_NEW_TASK: start the Activity in a new task. This behavior is

Intent flags ● FLAG_ACTIVITY_NEW_TASK: start the Activity in a new task. This behavior is the same as for single. Task launch mode. ● FLAG_ACTIVITY_SINGLE_TOP: if the Activity to be launched is at the top of the back stack, route the Intent to that existing Activity instance. Otherwise create a new Activity instance. This is the same behavior as the single. Top launch mode. ● FLAG_ACTIVITY_CLEAR_TOP: If an instance of the Activity to be launched already exists in the back stack, destroy any other Activity on top of it and route the Intent to that existing instance. ● When used in conjunction with FLAG_ACTIVITY_NEW_TASK, this flag locates any existing instances of the Activity in any task and brings it to the foreground. ● FLAG_ACTIVITY_CLEAR_TASK: Clear all activities in the task before start Implicit Intents Android Developer Fundamentals V 2 activity This work is licensed under a Creative Commons Attribution 4. 0 International License. 37

END Android Developer Fundamentals V 2 38

END Android Developer Fundamentals V 2 38