2011年5月20日金曜日

AndroidStudyMemo=What Are Intents?

Before you can begin to interact with the phone dialer, you need to understand the type

of code that you will use to do the job. Android uses Intents to do specific jobs within

applications. Once you master the use of Intents, a whole new world of application

development will be open to you. This section defines what an Intent is and how it is used.

 

An Intent is Android's method for relaying certain information from one Activity to

another. An Intent, in simpler terms, expresses to Android your intent to do something.

You can think of an Intent as a message passed between Activities. For example, assume

that you have an Activity that needs to open a web browser and display a page on your

Android device. Your Activity would send an "intent to open x page in the web browser,"

known as a WEB_SEARCH_ACTION Intent, to the Android Intent Resolver. The Intent

Resolver parses through a list of Activities and chooses the one that would best match

your Intent; in this case, the Web Browser Activity. The Intent Resolver then passes your

page to the web browser and starts the Web Browser Activity.

 

Intents are broken up into two main categories:

Activity Action Intents Intents used to call Activities outside of your application.

   Only one Activity can handle the Intent. For example, for a web browser, you need to

   open the Web Browser Activity to display a page.

Broadcast Intents Intents that are sent out for multiple Activities to handle. An

   example of a Broadcast Intent would be a message sent out by Android about the

   current battery level. Any Activity can process this Intent and react accordingly?for

   example, cancel an Activity if the battery level is below a certain point.

 

lists and describes the current Activity Action Intents that are available to

you. As you'll notice, in most cases, the name of the Intent does a good job of describing

what that Intent does.

 

              Activity Action Intent                                         Message     

              ADD_SHORTCUT_ACTION                               Add a function shortcut to the Android Home Screen

              ALL_APPS_ACTION                                        List all the applications available on the device 

              ANSWER_ACTION                                          Answer an incoming call     

              BUG_REPORT_ACTION                                   Open the Bug Reporting Activity    

              CALL_ACTION                                   Place a call to supplied location   

              DELETE_ACTION                              Delete the specified data     

              DIAL_ACTION                                    Open the Dial Activity and dial the specified number

              EDIT_ACTION                                    Provide editable access to the supplied data  

              EMERGENCY_DIAL_ACTION                                         Dial an emergency number     

              FACTORY_TEST_ACTION                                Retrieve factory test settings     

              GET_CONTENT_ACTION                                 Select and return specified data    

              INSERT_ACTION                               Insert an empty item     

              MAIN_ACTION                                   Establish the Activity start point    

              PICK_ACTION                                   Pick an item and return the selection  

              PICK_ACTIVITY_ACTION                                 Pick a given Activity (returns a class)  

              RUN_ACTION                                    Execute the given data     

              SEARCH_ACTION                             Launch a search on the system   

              SEND_ACTION                                  Send data without specifying the recipient   

              SENDTO_ACTION                             Send data to the recipient specified   

              SETTINGS_ACTION                                         Launch System Settings      

              SYNC_ACTION                                  Sync phone data with external source   

              VIEW_ACTION                                  (DEFAULT_ACTION) Open a View     

              WALLPAPER_SETTINGS_ACTION                                 Show settings for modifying the Android Wallpaper  

              WEB_SEARCH_ACTION                                  Open Google Search, or another web page if specified

 

 

 

lists and describes the current Broadcast Intents that are available. Refer to

this list when you need to establish a receiver for a specific Intent.

Broadcast Intent Message

              CALL_FORWARDING_STATE_CHANGED_ACTION The phone's call forwarding state has

              changed

              CAMERA_BUTTON_ACTION The camera button has been pressed

              CONFIGURATION_CHANGED_ACTION The device's configuration has changed

              DATA_ACTIVITY_STATE_CHANGED_ACTION The device's data activity state has changed

              DATA_CONNECTION_STATE_CHANGED_ACTION There has been a change in the data

              connection state

              DATE_CHANGED_ACTION The phone's system date has changed

              FOTA_CANCEL_ACTION Cancel pending system update downloads

              FOTA_INSTALL_ACTION An update has been downloaded and must

              be installed immediately (sent by the system)

              FOTA_READY_ACTION An update has been downloaded and can be

              installed?but does not need to be installed

              immediately (sent by the system)

              FOTA_RESTART_ACTION Restart a system update download

              FOTA_UPDATE_ACTION Begin the download of a system update

              GTALK_SERVICES_CONNECTED_ACTION Sent when a GTALK session has been

              successfully established

              GTALK_SERVICES_DISCONNECTED_ACTION Sent when a GTALK session has been

              disconnected

              MEDIA_BAD_REMOVAL_ACTION Sent when an SD Memory Card was

              removed but unsuccessfully unmounted from

              the system

              MEDIA_BUTTON_ACTION Sent when the media button has been

              pressed

              MEDIA_EJECT_ACTION Sent when the eject action has been initiated

              on an SD Memory Card

              MEDIA_MOUNTED_ACTION Sent when an SD Memory Card was

              successfully mounted to the system

              MEDIA_REMOVED_ACTION Sent when an SD memory card was detected

              as having been removed

              MEDIA_SCANNER_FINISHED_ACTION Sent when the scanner has finished

              MEDIA_SHARED_STARTED_ACTION Sent when the scanner has begun

              MEDIA_UNMOUNTED_ACTION Sent when an SD memory card has been

              detected but has not been mounted

              MESSAGE_WAITING_STATE_CHANGED The "message waiting" state on the phone

              has changed

              NETWORK_TICKLE_RECEIVED_ACTION A new device network notification has been

              received

              PACKAGE_ADDED_ACTION Sent when a new package has been installed

              on the device

              PACKAGE_CHANGE_ACTION Sent when an existing package has been

              modified

              PACKAGE_INSTALL_ACTION A package can be downloaded and installed

              PACKAGE_REMOVED_ACTION A package has been removed

              PHONE_INTERFACE_ADDED_ACTION The device's phone interface has been

              created

              PHONE_STATE_CHANGED_ACTION The device's phone state has changed

              PROVIDER_CHANGED_ACTION The device has received a notification from a

              provider

              PROVISIONING_CHECK_ACTION Check for the latest settings from the

              provisioning service

              SCREEN_OFF_ACTION The screen has been shut off (sent by the

              device)

              SCREEN_ON_ACTION The screen has been turned on (sent by the

              device)

              SERVICE_STATE_CHANGED_ACTION The service state has changed

              SIGNAL_STRENGTH_CHANGED_ACTION The signal strength has changed

              SIM_STATE_CHANGED_ACTION The state of the SIM card has changed

              TIME_CHANGED_ACTION The device's time was set

              TIME_TICK_ACTION The current time has changed

              TIMEZONE_CHANGED_ACTION The device's timezone has changed

              UMS_CONNECTED_ACTION The device has connected via USB

              UMS_DISCONNECTED_ACTION The device has been disconnected from its

              USB host

              WALLPAPER_CHANGED_ACTION The device's wallpaper has been changed

 

 

The Intent is only one-third of the picture. An Intent is really just that, an intent to do

something; an Intent cannot actually do anything by itself. You need Intent Filters and

Intent Receivers to listen for, and interpret, the Intents.

 

An Intent Receiver is like the mailbox of an Activity. The Intent Receiver is used

to allow an Activity to receive the specified Intent. Using the previous web browser

example, the Web Browser Activity is set up to receive web browser Intents. A system

like this allows unrelated Activities to ignore Intents that they would not be able to

process. It also allows Activities that need the assistance of another Activity to utilize

that Activity without needing to know how to call it.

 

With Intents and Intent Receivers, one Activity can send out an Intent and another can

receive it. However, there needs to be something that governs the type of information that

can be sent between the two Activities. This is where Intent Filters come in.

Intent Filters are used by Activities to describe the types of Intents they want to

receive. More importantly, they outline the type of data that should be passed with the

Intent. Therefore, in our example scenario, we want the web browser to open a web page.

The Intent Filter would state that the data passed with the WEB_SEARCH_ACTION

Intent should be in the form of a URL.

 

0 件のコメント:

コメントを投稿