ch5 intent broadcast receivers adapters and internet

57
Ch5. Intent, Broadcast Receivers, Adapters, and the internet Browny 16, May, 2011

Upload: browny-lin

Post on 19-May-2015

1.245 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Ch5 intent broadcast receivers adapters and internet

Ch5. Intent, Broadcast Receivers, Adapters, and

the internet

Browny16, May, 2011

Page 2: Ch5 intent broadcast receivers adapters and internet

Outline

• Introducing Intents

• Introducing Adapters

• Using Internet Resources

• Introducing Dialogs

Page 3: Ch5 intent broadcast receivers adapters and internet

Interaction

• Mobile applications on most platforms are isolated from each other, and have strict limitations applied to their interaction with hardware and native components

• Android applications use Intents, Broadcast Receivers, Adapters, Content Providers, and the Internet to interact through those boundaries

Page 4: Ch5 intent broadcast receivers adapters and internet

Intents

• A message-passing mechanism (within and between applications)

• Intents can be used to:‣ Declare  your  inten.on  that  an  Ac.vity  or  Service  

be  started  to  perform  an  ac.on,  usually  with  (or  on)  a  par.cular  piece  of  data

‣ Broadcast  that  an  event  (or  ac.on)  has  occurred

‣ Explicitly  start  a  par.cular  Service  or  Ac.vity

Page 5: Ch5 intent broadcast receivers adapters and internet

Using Intents to Launch Activities

• To open an Activity, call startActivity, passing in an Intent‣ Explicitly  (Explicitly  specify  the  Ac.vity  class  to  

open)

‣ Implicitly  (Include  an  ac.on  that  an  Ac.vity  must  perform)

• Intent resolution: Decide what activity is chosen to take that action (for implicitly)

Page 6: Ch5 intent broadcast receivers adapters and internet

Explicitly Starting New Activities (1/2)

1. Create a new Intent

2. Specifying the current application Context and Activity class to launch

3. Pass this Intent in to startActivity

Page 7: Ch5 intent broadcast receivers adapters and internet

Explicitly Starting New Activities(2/2)

• After startActivity is called, the new Activity will be created and become visible and active, moving to the top of the Activity stack

• Calling finish on the new Activity, or pressing the hardware back button, will close it and remove it from the stack

Page 8: Ch5 intent broadcast receivers adapters and internet

Implicit Intents (1/3)

• A mechanism that lets anonymous application components service action requests (without knowing exactly which application you’re borrowing functionality from)

• Android will at run time resolve it into the Activity class best suited to performing the required action on the type of data specified

Page 9: Ch5 intent broadcast receivers adapters and internet

Implicit Intents (2/3)

• Example: Let users make calls from your application‣ Implement  a  new  dialer  ?

‣ Use  an  implicit  Intent  that  requests  the  ac.on  (dialing)  be  performed  on  a  phone  number

Page 10: Ch5 intent broadcast receivers adapters and internet

Implicit Intents(3/3)

• Various native applications provide Activities to handle actions performed on specific data

• Third-party applications, including your own, can be registered ‣ To  support  new  ac.ons

‣ To  provide  an  alterna.ve  provider  of  na.ve  ac.ons

Page 11: Ch5 intent broadcast receivers adapters and internet

Returning Results from Activities

• startActivity will not provide any feedback

• You can start an Activity as a sub-Activity that’s inherently connected to its parent‣ A  sub-­‐Ac.vity  triggers  an  event  handler  within  

its  parent  Ac.vity  when  it  closes  

‣ 何時使用:  One  Ac.vity  is  providing  data  input  for  another

Page 12: Ch5 intent broadcast receivers adapters and internet

Sub-Activities

• Just Activities opened in a different way

• Must be registered in the application manifest

• Any manifest-registered Activity can be opened as a sub-Activity (system or third-party)

Page 13: Ch5 intent broadcast receivers adapters and internet

Launching Sub-Activities (1/3)

• startActivityForResult‣ Also  pass  in  a  request  code  (will  later  be  used  to  

uniquely  iden.fy  the  sub-­‐Ac.vity)

Page 14: Ch5 intent broadcast receivers adapters and internet

Launching Sub-Activities (2/3)

• Sub-Activities can be started implicitly or explicitly

• When your sub-Activity is ready to return, call setResult before finish to return a result to the calling Activity

Page 15: Ch5 intent broadcast receivers adapters and internet

Launching Sub-Activities(3/3)

• The setResult method takes 2 parameters: the result code and the result itself (represented as an Intent)

• Example: A sub-Activity’s onCreate method, and shows how an OK and Cancel button might return different results to the calling Activity (next page)

Page 16: Ch5 intent broadcast receivers adapters and internet

If the Activity is closed by the user pressing the hardware back key, or finish is called without a prior call to setResult, the result code will be set to RESULT_CANCELED and the result Intent set to null

Page 17: Ch5 intent broadcast receivers adapters and internet

Handling Sub-Activity Results

• When a sub-Activity closes, the onActivityResult event handler is fired within the calling Activity (override this method)

• The onActivityResult handler receives a number of parameters‣ Request  code

‣ Result  code

‣ Data

Page 18: Ch5 intent broadcast receivers adapters and internet
Page 19: Ch5 intent broadcast receivers adapters and internet

Native Android Actions

• Native Android applications also use Intents to launch Activities and sub-Activities

• When creating implicit Intents you can use these actions (called Activity Intents) to start Activities and sub-Activities within your own applications‣ ACTION_ANSWER,  ACTION_CALL,  ACTION_EDIT,  

ACTION_PICK,  etc...

Page 20: Ch5 intent broadcast receivers adapters and internet

Using Intent Filters to Service Implicit Intents

• How does Android know which application (and component) can service the request ?

• Intent Filters are used to register Activities, Services, and Broadcast Receivers as being capable of performing an action on a particular kind of data

Page 21: Ch5 intent broadcast receivers adapters and internet

Registering an Activity as an Intent Receiver(1/2)

• Add an <intent-filter> tag to the component’s manifest node using the following tags (and associated attributes) within the Intent Filter node

Page 22: Ch5 intent broadcast receivers adapters and internet

Registering an Activity as an Intent Receiver(2/2)

• action‣ Each  Intent  Filter  must  have  one  (and  only  one)  ac.on  

tag.  Ac.ons  should  be  unique  strings  that  are  self-­‐describing

• category‣ Specify  under  which  circumstances  the  ac.on  should  

be  serviced  (ALTERNATIVE,  SELECTIVE_ALTERNATIVE,  BROWSABLE,  etc...)

• data‣ Specify  which  data  types  your  component  can  act  on

Page 23: Ch5 intent broadcast receivers adapters and internet

How Android Resolves Intent Filters

1. Listing all the Intent Filters available from the installed packages

2. Matching the action or category associated with the Intent

3. Matching data

4. If more than one component is resolved from this process all the matching possibilities are offered to the user

Page 24: Ch5 intent broadcast receivers adapters and internet

Using the Launch Intent Within an Activity

• When an application component is started through an implicit Intent, it needs to find the action to be performed and the data to be performed on

• Use the getData and getAction methods to find the data and action associated with the Intent

Page 25: Ch5 intent broadcast receivers adapters and internet

Passing on Responsibility

• Use the startNextMatchingActivity method to pass responsibility for action handling to the next best matching application component

Page 26: Ch5 intent broadcast receivers adapters and internet

Using Intent Filters for Plug-Ins and Extensibility

• A plug-in model for your Activities that lets them take advantage of future functionality

• Using Intent Filters to populate menus dynamically at run time

Page 27: Ch5 intent broadcast receivers adapters and internet

Supplying Anonymous Actions to Applications (1/2)

• Make your Activity’s actions available anonymously for existing applications‣ Publish  them  using  intent-­‐filter  tags  within  their  

manifest  nodes

‣ The  category  tag  must  be  either  ALTERNATIVE  or  SELECTED_ALTERNATIVE  or  both

‣ The  text  used  for  the  Menu  Items  is  specified  by  the  android:label  aVribute

Page 28: Ch5 intent broadcast receivers adapters and internet

Supplying Anonymous Actions to Applications(2/2)

Page 29: Ch5 intent broadcast receivers adapters and internet

Incorporating Anonymous Actions in Your Activity’s Menu

...

Page 30: Ch5 intent broadcast receivers adapters and internet

Introducing Linkify

• Linkify is a helper class ‣ Automagically  creates  hyperlinks  within  Text  

View  (and  Text  View-­‐derived)  classes  through  RegEx  paVern  matching

‣ Text  that  matches  a  specified  RegEx  paVern  will  be  converted  into  a  clickable  hyperlink

‣ Implicitly  fires  startAc/vity(new  Intent(Intent.ACTION_VIEW,  uri)),  using  the  matched  text  as  the  target  URI

Page 31: Ch5 intent broadcast receivers adapters and internet

The Native Linkify Link Types

• How to linkify a Text View to display web and e-mail addresses as hyperlinks

• You can also linkify Views from within a layout resource using the android:autoLink attribute

Page 32: Ch5 intent broadcast receivers adapters and internet

Using Intents to Broadcast Events

• Intents are capable of sending structured messages across process boundaries

• By broadcasting an event using an Intent you let yourself and third-party developers react to events without having to modify your original application

• Android uses broadcast Intents to broadcast system events like battery-charging levels, network connections, and incoming calls

Page 33: Ch5 intent broadcast receivers adapters and internet

Broadcasting Events with Intents

• Construct the Intent you want to broadcast and use the sendBroadcast method to send it

• The Intent action string is used to identify the event being broadcast, so it should be a unique string that identifies the event

Page 34: Ch5 intent broadcast receivers adapters and internet

Broadcast Receivers(1/2)

• For a Broadcast Receiver, it needs to be registered, either in code or within the application manifest

Page 35: Ch5 intent broadcast receivers adapters and internet

Broadcast Receivers(2/2)

• Applications with Broadcast Receivers registered in the manifest don’t have to be running when the Intent is broadcast for the receivers to execute. They will be started automatically when a matching Intent is broadcast

• The five-second execution limit ensures that major processing cannot, and should not, be done within the Broadcast Receiver itself (update content, launch Services, update Activity UI, etc...)

Page 36: Ch5 intent broadcast receivers adapters and internet
Page 37: Ch5 intent broadcast receivers adapters and internet

Registering Broadcast Receivers in Your Application Manifest

Even when the application has been killed or hasn’t been started

Only when the application component it is registered within is running

Page 38: Ch5 intent broadcast receivers adapters and internet

Broadcasting Sticky and Ordered Intents

• Problem: When broadcasting an Intent using sendBroadcast, your Intent will be received by all registered Broadcast Receivers, but you cannot control the order and they cannot propagate results

Page 39: Ch5 intent broadcast receivers adapters and internet

Native Android Broadcast Actions

• You can use these messages to add functionality to your own projects based on system events such as time-zone changes, data-connection status, incoming SMS messages, or phone calls

• ACTION_CAMERA_BUTTON, ACTION_SCREEN_OFF, ACTION_TIMEZONE_CHANGED, etc...

Page 40: Ch5 intent broadcast receivers adapters and internet

Pending Intents

• The Pending Intent class provides a mechanism for creating Intents that can be fired by another application at a later time

• Commonly used to package an Intent that will be fired in response to a future event

Page 41: Ch5 intent broadcast receivers adapters and internet

Introducing Adapters

• Adapters are bridging classes that bind data to Views (such as List Views) used in the user interface

• Views that support Adapter binding must extend the AdapterView abstract class

• You can create your own AdapterView derived controls and to create new Adapter classes to bind them

Page 42: Ch5 intent broadcast receivers adapters and internet

Some Native Adapters

• ArrayAdapter‣ Array  Adapter  uses  the  toString  value  of  each  

object  in  the  array  to  create  and  populate  Text  Views

• SimpleCursorAdapter‣ You  specify  an  XML  layout  defini.on,  and  then  

bind  each  column  to  a  View  within  that  layout

Page 43: Ch5 intent broadcast receivers adapters and internet

Customizing the Array Adapter

• Customize the layout used to represent each View

• Extend ArrayAdapter with a type-specific variation, overriding the getView method to assign object properties to layout Views

Page 44: Ch5 intent broadcast receivers adapters and internet
Page 45: Ch5 intent broadcast receivers adapters and internet

Using Adapters for Data Binding

• Apply an Adapter to an AdapterView derived class: Call the View’s setAdapter, passing in an Adapter instance

Page 46: Ch5 intent broadcast receivers adapters and internet

Using Internet Resources

• Benefits (client native applications rather than entirely web-based solutions)‣ Bandwidth:  By  crea.ng  a  na.ve  applica.on  you  

can  limit  the  bandwidth  requirements  to  updated  data  only

‣ Caching:  A  na.ve  applica.on  can  cache  data  to  provide  as  much  func.onality  as  possible  without  a  live  connec.on

‣ Na4ve  features:  combine  the  data  available  online  with  the  hardware  features  available  on  the  device

Page 47: Ch5 intent broadcast receivers adapters and internet

Connecting to an Internet Resource

• Add an INTERNET uses-permission node to your application manifest‣ <uses-­‐permission  android:name="android.permission.  INTERNET"  />

Page 48: Ch5 intent broadcast receivers adapters and internet

Using Internet Resources

• 2 extreme‣ Include  a  WebKit-­‐based  browser  View  within  an  

Ac.vity

‣ Use  client-­‐side  APIs  to  interact  directly  with  server  processes

• Between‣ Process  remote  XML  feeds  to  extract  and  

process  data  using  a  Java-­‐based  XML  parser  such  as  SAX  or  the  more  efficient  XmlPul  lParser

Page 49: Ch5 intent broadcast receivers adapters and internet

Introducing Dialogs

• Answer questions

• Make selections, and confirm actions

• Display warning or error messages

Page 50: Ch5 intent broadcast receivers adapters and internet

Implement Dialog

• Using the Dialog class (or its extension)‣ Android  includes  a  number  of  specialist  classes  

that  extend  Dialog

‣ A  Dialog-­‐class-­‐based  screen  is  constructed  en.rely  within  its  calling  Ac.vity,  so  it  doesn’t  need  to  be  registered  in  the  manifest  (its  life  cycle  is  controlled  en.rely  by  the  calling  Ac.vity)

• Dialog-themed Activities

• Toasts

Page 51: Ch5 intent broadcast receivers adapters and internet

Introducing the Dialog Classes

Page 52: Ch5 intent broadcast receivers adapters and internet

The Alert Dialog Class

• Presenting a message to the user offering them one to three options in the form of buttons (OK, Cancel, Yes or No)

• Offering a list of options in the form of checkboxes or radio buttons

• Providing a text entry box for user input

Page 53: Ch5 intent broadcast receivers adapters and internet

Alert Dialog used to display a message and offer two button options to continue

Page 54: Ch5 intent broadcast receivers adapters and internet

Specialist Input Dialogs

• CharacterPickerDialog

• DatePickerDialog

• TimePickerDialog

• ProgressDialog

Page 55: Ch5 intent broadcast receivers adapters and internet

Using Activities as Dialogs

• When: Need more control over the content and life cycle of your dialog box‣ The  solu.on  is  to  implement  it  as  a  full  Ac.vity

• The easiest way to make an Activity look like a dialog is to apply the android:style/Theme.Dialog theme when you add it to your manifest

• Cause your Activity to behave as a Dialog, floating on top of, and partially obscuring, the Activity beneath it

Page 56: Ch5 intent broadcast receivers adapters and internet

Managing and Displaying Dialogs

• Creating new instances of a dialog each time it’s required (☹)

• Android provides the onCreateDialog and onPrepareDialog event handlers within the Activity class to persist and manage dialog-box instances

• showDialog -> onCreateDialog (if necessary) -> onPrepareDialog

Page 57: Ch5 intent broadcast receivers adapters and internet

Thank you :)