Ch 6: Intents and Broadcast Receivers Flashcards
Definition:
Intent
Intent
A message passing mechanism that can be used within an application, between applications, between the system and an application.
Intents are a fundamental part of the Android OS and are unique to Android.
Common Uses for Intents
- Explicitly start a particular Service, Broadcast Receiver, Activity or sub-Activity using its class name
- Start an Activity, sub-Activity or Service to perform an action with(or on) a particular piece of data
- Return information from a sub-Activity
- Broadcast that an event has occurred
Starting Activities
Explicitly
vs
Implicitly
- An Activity is started Explicitly using an Intent that specifies the name of the Activity class
- The Activity class must be within the calling application
- An Activity is started Implicitly when an Intent requests that a specific action be performed on a piece of data
- Android will dynamically choose an Activity or Service through the process of “Intent Resolution”
Explicitly Starting New Activities:
Steps
- Construct an Intent, specifying the current activity as context and the class of the Activity to be started
- Intent intent = new Intent( MyActivity.this, MyOtherActivity.class)
- Use the “startActivity()” method, with the Intent as a parameter, to submit a request to create, start and resume the Activity
- startActivity( intent )
Explicitly Starting New Activities:
How the Activity is Managed on the Activity Stack
- After “startActivity()” is called, the new Activity is created, started and resumed
- It is placed on top of the Activity stack
- Calling “finish()” on the new Activity, or pressing the hardware back button will close the Activity and remove it from the stack
- It may also navigate to other Activities using “startActivity()”
- Each call to “startActivity()” will create a new Instance of the Activity and add it to the stack
- If an Activity is closed without a call to finish, it might remain in the Activity Stack
- Multiple instances of the same Activity can exist in the Activity stack
Implicit Intent:
Definition
Implicit Intent
Used to ask the system to find and start an Activity that can perform a particular action, without you knowing exactly which application, or Activity, will be started.
Implicit Intent:
Constructing the Intent
- Uses an different constructor for Intent
- new Intent( Action , URI )
- Specify an Action
- There are several predefined Actions in the Intent class, ( Intent.ACTION_* )
- Specify the URI of the data on which the action should be performed
- After construction, may add additional primitive values to the Intent, called “Extras”
- Use the “putExtra()” method
- intent.putExtra(“STRING_EXTRA”, “My String”);
What is
“intent resolution” ?
When an Intent specifies a particular action to perform, rather than an Activity,
the run time will dynamically choose an Activity that is able to perform that action.
- This allows an app to use Activities from other applications, such as the dialer
- Some Activities have “Intent Filters” that help determine the Intent Resolution
How to recieve feedback/results
from launching a sub-activity
- Start the activity by passing the Intent to “startActivityForResult()” instead of “startActivity()”
- StartActivity does not provide any feedback
What are Broadcast Intents?
Intents that are used to broadcast messages across the system.
Applications can register Broadcast Receivers to listen for, and react to, the broadcast intents.
This enables event-driven applications.
The Android system uses Broadcast Intents to announce system events, such as changes in Internet Connectivity or Battery Level
Implicit Intents:
Extras
Extras
- A mechanism used to attach primitive values to an Intent
- Can use the overloaded “putExtra()” method on any Intent to attach a name-value pair
- Extras are stored within the Intent as Bundle objects
- Extract values from the bundle using the get[type]extra( name ) method
How to check at run time
if an Intent will be resolved?
- Use PackageManager to query which, if any, Activity would be launched:
- Get the Package Manager:
- PackageManager pm = getPackageManager();
- Pass it to the “resolveActivity()” method of the Intent Instance, which will return a ComponentName object
- ComponentName cn = intent.resolveActivity(pm)
- It will return null if no Activity is found
- This should be checked before passing the Intent to StartActivity()
Using a Sub-Activity:
Steps
- Launch:
- startActivityForResult( Intent , RequestCode)
- Preparing to Return:
- call setResult( ResultCode, ResultData)
- Call finish() to indicate that the sub-activity has completed
- Handle Results:
- onActivityResult() handler is triggered, with parameters:
- Request Code, Result Code, Data
Sub-Activities:
Important Methods and Parameters
Methods
- startActivityForResult()
- setResult()
- finish()
- onActivityResult() - event handler
- intent.resolveActivity()
Parameters/Values
- Request Code
- Result Code
- Data
Platform-Native Actions:
What are they?
A set of common Actions on the Android platform.
The Intent class has a set of static string constants used to represent the actions, these can be used when creating Implicit Intents to start Activities and sub-Activities.
- Example: ACTION_DIAL brings up the phone dialer Activity
Platform-Native Actions:
List of Common Action strings
defined in the Intent class
(10)
*Note: this is NOT comprehensive
- ACTION_DELETE
- ACTION_EDIT
- ACTION_INSERT
- ACTION_DIAL
- ACTION_PICK
- ACTION_SEARCH
- ACTION_SENDTO
- ACTION_SEND
- ACTION_VIEW
- ACTION_WEB_SEARCH
Platform-Native Actions:
ACTION_DELETE
Starts an Activity that lets you delete the data specified at the Intent’s data URI
Platform-Native Actions:
ACTION_DIAL
Brings up a dialer application.
With the number to dial pre-populated from the Intent’s data URI.
- By default this is handled by the native Android phone dialer
Platform-Native Actions:
ACTION_EDIT
Requests an Activity that can edit the data at the Intent’s data URI
Platform-Native Actions:
ACTION_INSERT
Opens an Activity capable of inserting new items into the Cursor specified in the Intent’s data URI
When called as a sub-Activity, it should return a URI to the newly inserted item.
Platform-Native Actions:
ACTION_PICK
Launches a sub-Activity that lets you pick an item from the Content Provider specified by the Intent’s data URI.
When closed, it should return a URI to the item that was picked.
The Activity launched depends on the data being picked.
Ex:
content://contacts/people will invoke the native contacts list
Platform-Native Actions:
ACTION_SEARCH
Typically used to launch a specific search Activity.
When it’s fired without a specific Activity, the user will be prompted to select from all applications that support search.
Supply the search term as a string in the Intent’s extras, using “SearchManager.QUERY” as the key.
Platform-Native Actions:
ACTION_SENDTO
Launches an Activity to send data to the contact specified by the Intent’s data URI.
Platform-Native Actions:
ACTION_SEND
Launches an Activity that sends the data specified by the Intent.
- The recipient contact needs to be selected by the resolved Activity.
- Use only to send data to a remote recipient ( not to another app on the device)
- Use “setType” to set the MIME type of the transmitted data
- The data itself should be stored as an extra by means of the key “EXTRA_TEXT” or “EXTRA_STREAM”, depending on the type
- In the case of email, the native Android applications will also accept extras via several other keys
Platform-Native Actions:
ACTION_VIEW
- Asks that the data supplied in the Intent’s data URI be viewed in the most reasonable manner
- This is the most common generic action
- Different applications will handle view requests depending on the URI schema of the data supplied:
- “http:” addresses open in the browser
- “tel:” addresses will open the dialer to call the number
- “geo:” addresses will be displayed in Google Maps
- “contact” content will be displayed in Contact Manager
Platform-Native Actions:
ACTION_WEB_SEARCH
Opens the Browser to perform a web search based on the query supplied using the SearchManager.QUERY key
Intent Filter:
Basic Idea
An Intent Filter is a declaration that a particular Activity is capable of performing an action on a type of data.
They are also used by Broadcast Recievers to indicate the broadcast actions that will be recieved