Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers...

22
lec 04 Intents and Bundles Fragments

Transcript of Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers...

Page 1: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

lec 04

Intents and Bundles

Fragments

Page 2: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Activated by Intents

Activities

Services

Broadcast Receivers (aka Receivers)

(http://developer.android.com/guide/components/intents-filters.html)

Page 3: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Intent Architecture//Explicit; all you need is the packageContext and the target class

Intent itn = new Intent(this, ResultActivity.class);

ResultActivity.class

Explicit

Page 4: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Intent Architecture//Explicit; all you need is the packageContext and the target class Intent itn = new Intent(this, ResultActivity.class); itn.putExtra(QuizActivity.CORRECT, mCorrect); itn.putExtra(QuizActivity.INCORRECT, mIncorrect); itn.putExtra(QuizActivity.NAME, mName);

CORRECT: 5

INCORRECT: 3

PLAYER: Adam

ResultActivity.class

Explicit

Page 5: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Intent Architecture::Action/Data/etc//Explicit; all you need is the ComponentName and optionally the bundle.//Implicit; usually Action/Data and then optionally the bundle.

name : Adam Gerber

email : [email protected]

ret : something...

Action

Data

Scheme

Categories

ComponentNameExplicit

Implicit

Page 6: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.
Page 7: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.
Page 8: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

ADB (Android Device Bridge) adb kill-server adb start-server adb get-state adb devices

Use kill-server / start-server to reboot the adb if your device/emulator is not communicating with your dev-machine.

Page 9: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Explicit Intents: intra-app call by name

//********************************//This method works great for intra-app calls between

activities//********************************

//use the calling class and the called class as params to constructor.

Intent itn = new Intent(this, Second.class);startActivity(itn);

//********************************//You can use this method as well//notice that all you need is the component name for

explicit// calls//********************************

//instantiate with zero params and add the component this wayIntent itn = new Intent();itn.setComponent(new ComponentName("edu.uchicago.cs",

"edu.uchicago.cs.Second"));startActivity(itn);

Page 10: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Explicit Intents: intra-app call by name

// this is inside the another method, such as the onClick method

Intent itnThird = new Intent();itnThird.setComponent(new

ComponentName("edu.uchicago.cs", "edu.uchicago.cs.Third"));//********************************// or you could do this -> Intent itnThird = new

Intent(this, Third.class);//********************************itnThird.putExtra("name", "Adam Gerber");itnThird.putExtra("email", "[email protected]");

startActivity(itnThird);

Page 11: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Implicit Intents: inter-app call

Intent itn = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));startActivity(itn);

Implicit intents are anonymous and loosely-coupled.

You request the component like a service of the operating system.

Page 12: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Implicit Intents: Action/Data pairsACTION_VIEW content://contacts/people/1 -- Display information about the person whose identifier is "1".

ACTION_DIAL content://contacts/people/1 -- Display the phone dialer with the person filled in.

ACTION_VIEW tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.

ACTION_DIAL tel:123 -- Display the phone dialer with the given number filled in.

ACTION_EDIT content://contacts/people/1 -- Edit information about the person whose identifier is "1".

ACTION_VIEW content://contacts/people/ -- Display a list of people, which the user can browse through. This example is a typical top-level entry into the Contacts application, showing you the list of people.

Page 13: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Intent Architecture::Bundle (aka extras)//The bundle is a data structure inside the Intent.//It holds key/value pairs.//You can use predefined keys as well.//If you don't place the extras in there

name : Adam Gerber

email : [email protected]

ret : something...

Action

Data

Scheme

Categories

ComponentName

Page 14: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

IntentsIntent messaging is a facility for late run-time binding

between components in the same or different applications.

Intents are handled by the Android OS. In order for the OS to know about a component, it must be declared in the application manifest file.

If a component does not define Intent filters, it can only be called by explicit Intents. A component with filters can receive both explicit and implicit intents.

6/12/12

Page 15: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

AndroidManifest.xml fileIs an inventory of all the components in an

application.

If the component is not listed there, the Android OS won't know it's there. Not even intra-app component communication will resolve.

6/12/12

Page 16: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Android OSAndroid OS

M

SA

*A I

M

A

*A

R

CP

Page 17: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Intent Filters::Two purposes1/ inventory of components used by the OS that

are callable given certain criteria.

2/ filter requests. You can define from course to fine

Beware, there is a slight mis-matching between the java and xml:

ACTION_WEB_SEARCH corresponds to android.intent.action.WEB_SEARCH

ACTION_EDIT corresponds to android.intent.action.EDIT

ACTION_EDIT corresponds to android.intent.action.EDIT

CATEGORY_BROWSABLE corresonds to android.intent.category.BROWSABLE

Page 18: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Categories

//Use Categories to further refine your Intent//Most important is CATEGORY_DEFAULT

<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>

Page 19: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

From Google: An Intent object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity)

Page 20: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Only three aspects of an Intent object are consulted when the object is tested against an intent filter:

>action >data (both URI and data type) >category

must pass all three tests.

A component can have multiple intent filters.

a filter must contain at least one <action> element, or it will block all intents

Page 21: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

Adapters

Page 22: Lec 04 Intents and Bundles Fragments. Activated by Intents Activities Services Broadcast Receivers (aka Receivers) (.

AdapterViews