Turning Your Application into a Home-Screen Widget

169

Chapter 7: Turning Your Application into a Home-Screen Widget

✓ Data: The data to operate on, such as a record in a database or a uni- form resource identifier that should be opened, such as a Web site URL. Table 7-1 demonstrates a few action and data parameters for Intent objects and their simple data structure. Table 7-1 Intent Data Examples Action Data Result ACTION_VIEW tel:123 Display the dialer with the given number 123 filled in to the dialer ACTION_DIAL content: contacts people1 Display the dialer with the phone number from the contact with the ID of 1 in the dialer ACTION_EDIT content: contacts people1 Edit the information about the person whose given identifier is 1 ACTION_VIEW http:www. example.org Display the Web page of the given intent ACTION_VIEW content: contacts people Display a list of all the people in the contacts system Intents can also carry an array of other data that include the following: ✓ category: Gives additional information about the action to execute. As an example, if CATEGORY_LAUNCHER is present, it means that the appli- cation should show up in the application launcher as a top-level applica- tion. Another option is CATEGORY_ALTERNATIVE, which can provide alternative actions that the user can perform on a piece of data. ✓ type: Specifies a particular type MIME type of the intent data. An example would be setting the type to audiompeg — the Android system would recognize that you are working with an MP3 file. Normally the type is inferred by the data itself. By setting this, you override the inferred type inference by explicitly setting it in the intent. ✓ component: Specifies an explicit component name of the class to execute the intent upon. Normally, the component is inferred by inspection of other information in the intent action, datatype, and categories, and the matching components can handle it. If this attribute is set, none of that evaluation takes place, and this component is used exactly as speci- fied. This is probably going to be the most common use case in your applications. You can provide another activity as the component — this addresses Android to interact with that specific class. 170 Part II: Building and Publishing Your First Android Application ✓ extras: A bundle of additional information that is key based. This is used to provide extra information to the receiving component. For example, if you needed to send an e-mail address, you use the extras bundle to supply the body, subject, and other components of the e-mail. Evaluating intents Intents are evaluated in the Android system in one of two ways: ✓ Explicitly: The intent has specified an explicit component or the exact class that will execute the data in the intent again, this will probably be the most common way you address intents. These types of intents often contain no other data because they are a means to start other activities within an application. I show you how to use an explicit intent in this application later in the chapter. ✓ Implicitly: The intent has not specified a component or class. Instead, the intent must provide enough information about the action that needs to be performed with the given data for the Android system to determine which available components can handle the intent. This is sometimes referred to as an address and payload. An example of this would be setting up an e-mail intent that contains e-mail fields To, CC, Subject, and Body and an e-mail MIME type. Android interprets this as an e-mail and gives the user of the device the opportunity to choose which application should handle the intent. A couple of possibilities include Gmail, Exchange, or a POP e-mail account that are all enabled on the device. This allows the user to determine where the e-mail should originate from. Android’s feature of identifying the possible matches for the given intent is known as intent resolution. Using pending intents A PendingIntent is an intent at the core, but with a slight paradigm shift in regard to functionality. A PendingIntent is created by your application and given to another completely different application. By giving another applica- tion a PendingIntent, you’re granting the other application the right to perform the operation you have specified as if the application was your application. In laymen’s terms, you are giving information on how to call your application to perform work on another application’s behalf. When the other application deems that the given work needs to take place, it executes the PendingIntent, which instructs the Android messaging system to inform your application to perform the necessary work. 171

Chapter 7: Turning Your Application into a Home-Screen Widget