1 page.title=Intents and Intent Filters 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <div id="qv"> 6 7 <h2>In this document</h2> 8 <ol> 9 <li><a href="#iobjs">Intent Objects</a></li> 10 <li><a href="#ires">Intent Resolution</a></li> 11 <li style="margin-left: 2em"><a href="#ifs">Intent filters</a></li> 12 <li style="margin-left: 2em"><a href="#ccases">Common cases</a></li> 13 <li style="margin-left: 2em"><a href="#imatch">Using intent matching</a></li> 14 <li><a href="#npex">Note Pad Example</a></li> 15 </ol> 16 17 <h2>Key classes</h2> 18 <ol> 19 <li>{@link android.content.Intent}</li> 20 <li>{@link android.content.IntentFilter}</li> 21 <li>{@link android.content.BroadcastReceiver}</li> 22 <li>{@link android.content.pm.PackageManager}</li> 23 </ol> 24 25 </div> 26 </div> 27 28 29 <p> 30 Three of the core components of an application — activities, services, and 31 broadcast receivers — are activated through messages, called <i>intents</i>. 32 Intent messaging is a facility for late run-time binding between components in the same 33 or different applications. The intent itself, an {@link android.content.Intent} 34 object, is a passive data structure holding an abstract description of an operation 35 to be performed — or, often in the case of broadcasts, a description of something 36 that has happened and is being announced. There are separate mechanisms for 37 delivering intents to each type of component: 38 </p> 39 40 <ul> 41 <li>An Intent object is passed to <code>{@link android.content.Context#startActivity 42 Context.startActivity()}</code> or <code>{@link android.app.Activity#startActivityForResult 43 Activity.startActivityForResult()}</code> to launch an activity or get an existing 44 activity to do something new. (It can also be passed to 45 <code>{@link android.app.Activity#setResult(int, Intent) Activity.setResult()}</code> 46 to return information to the activity that called {@code startActivityForResult()}.)</li> 47 48 <li><p>An Intent object is passed to <code>{@link android.content.Context#startService 49 Context.startService()}</code> to initiate a service or deliver new instructions to an 50 ongoing service. Similarly, an intent can be passed to <code>{@link 51 android.content.Context#bindService Context.bindService()}</code> to establish a 52 connection between the calling component and a target service. It can optionally 53 initiate the service if it's not already running.</p></li> 54 55 <li><p>Intent objects passed to any of the broadcast methods (such as <code>{@link 56 android.content.Context#sendBroadcast(Intent) Context.sendBroadcast()}</code>, 57 <code>{@link android.content.Context#sendOrderedBroadcast(Intent, String) 58 Context.sendOrderedBroadcast()}</code>, or <code>{@link 59 android.content.Context#sendStickyBroadcast Context.sendStickyBroadcast()}</code>) 60 are delivered to all interested broadcast receivers. Many kinds of broadcasts 61 originate in system code.</p></li> 62 </ul> 63 64 <p> 65 In each case, the Android system finds the appropriate activity, service, or set 66 of broadcast receivers to respond to the intent, instantiating them if necessary. 67 There is no overlap within these messaging systems: Broadcast intents are delivered 68 only to broadcast receivers, never to activities or services. An intent passed to 69 {@code startActivity()} is delivered only to an activity, never to a service or 70 broadcast receiver, and so on. 71 </p> 72 73 <p> 74 This document begins with a description of Intent objects. It then describes the 75 rules Android uses to map intents to components — how it resolves which 76 component should receive an intent message. For intents that don't explicitly 77 name a target component, this process involves testing the Intent object against 78 <i>intent filters</i> associated with potential targets. 79 </p> 80 81 82 <h2><a name="iobjs"></a>Intent Objects</h2> 83 84 <p> 85 An {@link android.content.Intent} object is a bundle of information. It 86 contains information of interest to the component that receives the intent 87 (such as the action to be taken and the data to act on) plus information 88 of interest to the Android system (such as the category of component that 89 should handle the intent and instructions on how to launch a target activity). 90 Principally, it can contain the following: 91 </p> 92 93 <dl> 94 95 <dt><b>Component name</b><a name="cname"></a></dt> 96 <dd>The name of the component that should handle the intent. This field is 97 a {@link android.content.ComponentName} object — a combination of the 98 fully qualified class name of the target component (for example "{@code 99 com.example.project.app.FreneticActivity}") and the package name set 100 in the manifest file of the application where the component resides (for 101 example, "{@code com.example.project}"). The package part of the component 102 name and the package name set in the manifest do not necessarily have to match. 103 104 <p> 105 The component name is optional. If it is set, the Intent object is 106 delivered to an instance of the designated class. If it is not set, 107 Android uses other information in the Intent object to locate a suitable 108 target — see <a href="#ires">Intent Resolution</a>, later in this 109 document. 110 </p> 111 112 <p> 113 The component name is set by <code>{@link android.content.Intent#setComponent 114 setComponent()}</code>, <code>{@link android.content.Intent#setClass 115 setClass()}</code>, or <code>{@link android.content.Intent#setClassName(String, String) 116 setClassName()}</code> and read by <code>{@link android.content.Intent#getComponent 117 getComponent()}</code>. 118 </p> 119 </dd> 120 121 <p><dt><b>Action</b></dt> 122 <dd>A string naming the action to be performed — or, in the case of broadcast 123 intents, the action that took place and is being reported. The Intent class defines 124 a number of action constants, including these: 125 </p> 126 127 <table> 128 <tr> 129 <th>Constant</th> 130 <th>Target component</th> 131 <th>Action</th> 132 </tr><tr> 133 <td>{@code ACTION_CALL} 134 <td>activity 135 <td>Initiate a phone call. 136 </tr><tr> 137 <td>{@code ACTION_EDIT} 138 <td>activity 139 <td>Display data for the user to edit. 140 </tr><tr> 141 <td>{@code ACTION_MAIN} 142 <td>activity 143 <td>Start up as the initial activity of a task, with no data input and no returned output. 144 </tr><tr> 145 <td>{@code ACTION_SYNC} 146 <td>activity 147 <td>Synchronize data on a server with data on the mobile device. 148 </tr><tr> 149 <td>{@code ACTION_BATTERY_LOW} 150 <td>broadcast receiver 151 <td>A warning that the battery is low. 152 </tr><tr> 153 <td>{@code ACTION_HEADSET_PLUG} 154 <td>broadcast receiver 155 <td>A headset has been plugged into the device, or unplugged from it. 156 </tr><tr> 157 <td>{@code ACTION_SCREEN_ON} 158 <td>broadcast receiver 159 <td>The screen has been turned on. 160 </tr><tr> 161 <td>{@code ACTION_TIMEZONE_CHANGED} 162 <td>broadcast receiver 163 <td>The setting for the time zone has changed. 164 </tr> 165 </table> 166 167 <p> 168 See the {@link android.content.Intent} class description for a list of 169 pre-defined constants for generic actions. Other actions are defined 170 elsewhere in the Android API. 171 You can also define your own action strings for activating the components 172 in your application. Those you invent should include the application 173 package as a prefix — for example: 174 "<code>com.example.project.SHOW_COLOR</code>". 175 </p> 176 177 <p> 178 The action largely determines how the rest of the intent is structured 179 — particularly the <a href="#data">data</a> and 180 <a href="#extras">extras</a> fields — 181 much as a method name determines a set of arguments and a return value. 182 For this reason, it's a good idea to use action names that are 183 as specific as possible, and to couple them tightly to the other fields of 184 the intent. In other words, instead of defining an action in isolation, 185 define an entire protocol for the Intent objects your components can handle. 186 </p> 187 188 <p> 189 The action in an Intent object is set by the 190 <code>{@link android.content.Intent#setAction setAction()}</code> 191 method and read by 192 <code>{@link android.content.Intent#getAction getAction()}</code>. 193 </p> 194 </dd> 195 196 <p><dt><b>Data</b><a name="data"></a></dt> 197 <dd>The URI of the data to be acted on and the MIME type of that data. Different 198 actions are paired with different kinds of data specifications. For example, if 199 the action field is {@code ACTION_EDIT}, 200 the data field would contain the URI of the document to be displayed for editing. 201 If the action is {@code ACTION_CALL}, the data field would be a {@code tel:} URI 202 with the number to call. Similarly, if the action is {@code ACTION_VIEW} and the 203 data field is an {@code http:} URI, the receiving activity would be called upon 204 to download and display whatever data the URI refers to. 205 206 <p> 207 When matching an intent to a component that is capable of handling the data, 208 it's often important to know the type of data (its MIME type) in addition to its URI. 209 For example, a component able to display image data should not be called 210 upon to play an audio file. 211 </p> 212 213 <p> 214 In many cases, the data type can be inferred from the URI — particularly 215 {@code content:} URIs, which indicate that the data is located on the device and 216 controlled by a content provider (see the 217 <a href="{@docRoot}guide/topics/providers/content-providers.html">separate 218 discussion on content providers</a>). But the type can also be explicitly set 219 in the Intent object. 220 The <code>{@link android.content.Intent#setData setData()}</code> method specifies 221 data only as a URI, <code>{@link android.content.Intent#setType setType()}</code> 222 specifies it only as a MIME type, and <code>{@link 223 android.content.Intent#setDataAndType setDataAndType()}</code> specifies it as both 224 a URI and a MIME type. The URI is read by <code>{@link 225 android.content.Intent#getData getData()}</code> and the type by <code>{@link 226 android.content.Intent#getType getType()}</code>. 227 </p> 228 </dd> 229 230 <p><dt><b>Category</b></dt> 231 <dd>A string containing additional information about the kind of component 232 that should handle the intent. Any number of category descriptions can be 233 placed in an Intent object. As it does for actions, the Intent class defines 234 several category constants, including these: 235 236 <table> 237 <tr> 238 <th>Constant</th> 239 <th>Meaning</th> 240 </tr><tr> 241 <td>{@code CATEGORY_BROWSABLE} 242 <td>The target activity can be safely invoked by the browser to display data 243 referenced by a link — for example, an image or an e-mail message. 244 </tr><tr> 245 <td>{@code CATEGORY_GADGET} 246 <td>The activity can be embedded inside of another activity that hosts gadgets. 247 </tr><tr> 248 <td>{@code CATEGORY_HOME} 249 <td>The activity displays the home screen, the first screen the user sees when 250 the device is turned on or when the <em>Home</em> button is pressed. 251 </tr><tr> 252 <td>{@code CATEGORY_LAUNCHER} 253 <td>The activity can be the initial activity of a task and is listed in 254 the top-level application launcher. 255 </tr><tr> 256 <td>{@code CATEGORY_PREFERENCE} 257 <td>The target activity is a preference panel. 258 </tr> 259 </table> 260 261 <p> 262 See the {@link android.content.Intent} class description for the full list of 263 categories. 264 </p> 265 266 <p> 267 The <code>{@link android.content.Intent#addCategory addCategory()}</code> method 268 places a category in an Intent object, <code>{@link android.content.Intent#removeCategory 269 removeCategory()}</code> deletes a category previously added, and <code>{@link android.content.Intent#getCategories getCategories()}</code> gets the set of all 270 categories currently in the object. 271 </p> 272 </dd> 273 274 <p><dt><b>Extras</b><a name="extras"></a></dt> 275 <dd>Key-value pairs for additional information that should be delivered to the 276 component handling the intent. Just as some actions are paired with particular 277 kinds of data URIs, some are paired with particular extras. For example, an 278 {@code ACTION_TIMEZONE_CHANGED} intent has a "{@code time-zone}" extra that 279 identifies the new time zone, and {@code ACTION_HEADSET_PLUG} has a 280 "{@code state}" extra indicating whether the headset is now plugged in or 281 unplugged, as well as a "{@code name}" extra for the type of headset. 282 If you were to invent a {@code SHOW_COLOR} action, the color value would 283 be set in an extra key-value pair. 284 285 <p> 286 The Intent object has a series of {@code put...()} methods for inserting various 287 types of extra data and a similar set of {@code get...()} methods for reading 288 the data. These methods parallel those for {@link android.os.Bundle} objects. 289 In fact, the extras can be installed and read as a Bundle using the <code>{@link 290 android.content.Intent#putExtras putExtras()}</code> and <code>{@link 291 android.content.Intent#getExtras getExtras()}</code> methods. 292 </p> 293 </dd> 294 295 <p><dt><b>Flags</b></dt> 296 <dd>Flags of various sorts. Many instruct the Android system how to launch an 297 activity (for example, which task the activity should belong to) and how to treat 298 it after it's launched (for example, whether it belongs in the list of recent 299 activities). All these flags are defined in the Intent class. 300 </dd> 301 302 </dl> 303 304 <p> 305 The Android system and the applications that come with the platform employ 306 Intent objects both to send out system-originated broadcasts and to activate 307 system-defined components. To see how to structure an intent to activate a 308 system component, consult the 309 <a href="{@docRoot}guide/appendix/g-app-intents.html">list of intents</a> 310 in the reference. 311 </p> 312 313 314 <h2><a name="ires"></a>Intent Resolution</h2> 315 316 <p> 317 Intents can be divided into two groups: 318 </p> 319 320 <ul> 321 <li><i>Explicit intents</i> designate the target component by its 322 name (the <a href="#cname">component name field</a>, mentioned earlier, 323 has a value set). Since component names would generally not be known to 324 developers of other applications, explicit intents are typically used 325 for application-internal messages — such as an activity starting 326 a subordinate service or launching a sister activity.</li> 327 328 <li><p><i>Implicit intents</i> do not name a target (the field for 329 the component name is blank). Implicit intents are often used to 330 activate components in other applications.</p></li> 331 </ul> 332 333 <p> 334 Android delivers an explicit intent to an instance of the designated 335 target class. Nothing in the Intent object other than the component 336 name matters for determining which component should get the intent. 337 </p> 338 339 <p> 340 A different strategy is needed for implicit intents. In the absence of a 341 designated target, the Android system must find the best component (or 342 components) to handle the intent — a single activity or service to 343 perform the requested action or the set of broadcast receivers to respond 344 to the broadcast announcement. It does so by comparing the contents of 345 the Intent object to <i>intent filters</i>, structures associated with 346 components that can potentially receive intents. Filters advertise the 347 capabilities of a component and delimit the intents it can handle. They 348 open the component to the possibility of receiving implicit intents of 349 the advertised type. If a component does not have any intent filters, 350 it can receive only explicit intents. A component with filters can 351 receive both explicit and implicit intents. 352 </p> 353 354 <p> 355 Only three aspects of an Intent object are consulted when the object 356 is tested against an intent filter: 357 </p> 358 359 <p style="margin-left: 2em">action 360 <br/>data (both URI and data type) 361 <br/>category</p> 362 363 <p> 364 The extras and flags play no part in resolving which component receives 365 an intent. 366 </p> 367 368 369 <h3><a name="ifs"></a>Intent filters</h3> 370 371 <p> 372 To inform the system which implicit intents they can handle, activities, 373 services, and broadcast receivers can have one or more intent filters. 374 Each filter describes a capability of the component, a set of intents that 375 the component is willing to receive. It, in effect, filters in 376 intents of a desired type, while filtering out unwanted 377 intents — but only unwanted implicit intents (those that don't name 378 a target class). An explicit intent is always delivered to its target, 379 no matter what it contains; the filter is not consulted. But an implicit 380 intent is delivered to a component only if it can pass through one of the 381 component's filters. 382 </p> 383 384 <p> 385 A component has separate filters for each job it can do, each face it can 386 present to the user. For example, the NoteEditor activity of the sample 387 Note Pad application has two filters — one for starting up with a 388 specific note that the user can view or edit, and another for starting 389 with a new, blank note that the user can fill in and save. (All of Note 390 Pad's filters are described in the <a href="#npex">Note Pad Example</a> 391 section, later.) 392 </p> 393 394 <div class="sidebox-wrapper"> 395 <div class="sidebox"> 396 <h2>Filters and security</h2> 397 <p>An intent filter cannot be relied on for security. While it opens a 398 component to receiving only certain kinds of implicit intents, it does 399 nothing to prevent explicit intents from targeting the component. Even 400 though a filter restricts the intents a component will be asked to handle 401 to certain actions and data sources, someone could always put 402 together an explicit intent with a different action and data source, and 403 name the component as the target. 404 </p> 405 </div> 406 </div> 407 408 <p> 409 An intent filter is an instance of the {@link android.content.IntentFilter} class. 410 However, since the Android system must know about the capabilities of a component 411 before it can launch that component, intent filters are generally not set up in 412 Java code, but in the application's manifest file (AndroidManifest.xml) as 413 <code><a href="{@docRoot}guide/topics/manifest/intent-filter-element.html"><intent-filter></a></code> 414 elements. (The one exception would be filters for 415 broadcast receivers that are registered dynamically by calling <code>{@link android.content.Context#registerReceiver(BroadcastReceiver, IntentFilter, String, 416 Handler) Context.registerReceiver()}</code>; they are directly created as 417 IntentFilter objects.) 418 </p> 419 420 <p> 421 A filter has fields that parallel the action, data, and category fields of an 422 Intent object. An implicit intent is tested against the filter in all three areas. 423 To be delivered to the component that owns the filter, it must pass all three tests. 424 If it fails even one of them, the Android system won't deliver it to the 425 component — at least not on the basis of that filter. However, since a 426 component can have multiple intent filters, an intent that does not pass 427 through one of a component's filters might make it through on another. 428 </p> 429 430 <p> 431 Each of the three tests is described in detail below: 432 </p> 433 434 <dl> 435 436 <dt><b>Action test</b></dt> 437 <dd>An 438 <code><a href="{@docRoot}guide/topics/manifest/intent-filter-element.html"><intent-filter></a></code> 439 element in the manifest file lists actions as 440 <code><a href="{@docRoot}guide/topics/manifest/action-element.html"><action></a></code> 441 subelements. For example: 442 443 <pre><intent-filter . . . > 444 <action android:name="com.example.project.SHOW_CURRENT" /> 445 <action android:name="com.example.project.SHOW_RECENT" /> 446 <action android:name="com.example.project.SHOW_PENDING" /> 447 . . . 448 </intent-filter></pre> 449 450 <p> 451 As the example shows, while an Intent object names just a single action, 452 a filter may list more than one. The list cannot be empty; a filter must 453 contain at least one {@code <action>} element, or it 454 will block all intents. 455 </p> 456 457 <p> 458 To pass this test, the action specified in the Intent object must match 459 one of the actions listed in the filter. If the object or the filter 460 does not specify an action, the results are as follows: 461 </p> 462 463 <ul> 464 <li>If the filter fails to list any actions, there is nothing for an 465 intent to match, so all intents fail the test. No intents can get 466 through the filter.</li> 467 468 <li><p>On the other hand, an Intent object that doesn't specify an 469 action automatically passes the test — as long as the filter 470 contains at least one action.</p></li> 471 </ul 472 </dd> 473 474 <dt><b>Category test</b></dt> 475 <dd>An 476 <code><a href="{@docRoot}guide/topics/manifest/intent-filter-element.html"><intent-filter></a></code> 477 element also lists categories as subelements. For example: 478 479 <pre><intent-filter . . . > 480 <category android:name="android.intent.category.DEFAULT" /> 481 <category android:name="android.intent.category.BROWSABLE" /> 482 . . . 483 </intent-filter></pre> 484 485 <p> 486 Note that the constants described earlier for actions and categories are not 487 used in the manifest file. The full string values are used instead. For 488 instance, the "{@code android.intent.category.BROWSABLE}" string in the example 489 above corresponds to the {@code CATEGORY_BROWSABLE} constant mentioned earlier 490 in this document. Similarly, the string "{@code android.intent.action.EDIT}" 491 corresponds to the {@code ACTION_EDIT} constant. 492 </p> 493 494 <p> 495 For an intent to pass the category test, every category in the Intent object 496 must match a category in the filter. The filter can list additional categories, 497 but it cannot omit any that are in the intent. 498 </p> 499 500 <p> 501 In principle, therefore, an Intent object with no categories should always pass 502 this test, regardless of what's in the filter. That's mostly true. However, 503 with one exception, Android treats all implicit intents passed to {@link 504 android.content.Context#startActivity startActivity()} as if they contained 505 at least one category: "{@code android.intent.category.DEFAULT}" (the 506 {@code CATEGORY_DEFAULT} constant). 507 Therefore, activities that are willing to receive implicit intents must 508 include "{@code android.intent.category.DEFAULT}" in their intent filters. 509 (Filters with "{@code android.intent.action.MAIN}" and 510 "{@code android.intent.category.LAUNCHER}" settings are the exception. 511 They mark activities that begin new tasks and that are represented on the 512 launcher screen. They can include "{@code android.intent.category.DEFAULT}" 513 in the list of categories, but don't need to.) See <a href="#imatch">Using 514 intent matching</a>, later, for more on these filters.) 515 </p> 516 <dd> 517 518 <dt><b>Data test</b></dt> 519 <dd>Like the action and categories, the data specification for an intent filter 520 is contained in a subelement. And, as in those cases, the subelement can appear 521 multiple times, or not at all. For example: 522 523 <pre><intent-filter . . . > 524 <data android:mimeType="video/mpeg" android:scheme="http" . . . /> 525 <data android:mimeType="audio/mpeg" android:scheme="http" . . . /> 526 . . . 527 </intent-filter></pre> 528 529 <p> 530 Each 531 <code><a href="{@docRoot}guide/topics/manifest/data-element.html"><data></a></code> 532 element can specify a URI and a data type (MIME media type). There are separate 533 attributes — {@code scheme}, {@code host}, {@code port}, 534 and {@code path} — for each part of the URI: 535 </p> 536 537 <p style="margin-left: 2em">{@code scheme://host:port/path}</p> 538 539 <p> 540 For example, in the following URI, 541 </p> 542 543 <p style="margin-left: 2em">{@code content://com.example.project:200/folder/subfolder/etc}</p> 544 545 <p> the scheme is "{@code content}", the host is "{@code com.example.project}", 546 the port is "{@code 200}", and the path is "{@code folder/subfolder/etc}". 547 The host and port together constitute the URI <i>authority</i>; if a host is 548 not specified, the port is ignored. 549 </p> 550 551 <p> 552 Each of these attributes is optional, but they are not independent of each other: 553 For an authority to be meaningful, a scheme must also be specified. 554 For a path to be meaningful, both a scheme and an authority must be specified. 555 </p> 556 557 <p> 558 When the URI in an Intent object is compared to a URI specification in a filter, 559 it's compared only to the parts of the URI actually mentioned in the filter. 560 For example, if a filter specifies only a scheme, all URIs with that scheme match 561 the filter. If a filter specifies a scheme and an authority but no path, all URIs 562 with the same scheme and authority match, regardless of their paths. If a filter 563 specifies a scheme, an authority, and a path, only URIs with the same scheme, 564 authority, and path match. However, a path specification in the filter can 565 contain wildcards to require only a partial match of the path. 566 </p> 567 568 <p> 569 The {@code type} attribute of a {@code <data>} element specifies the MIME type 570 of the data. It's more common in filters than a URI. Both the Intent object and 571 the filter can use a "*" wildcard for the subtype field — for example, 572 "{@code text/*}" or "{@code audio/*}" — indicating any subtype matches. 573 </p> 574 575 <p> 576 The data test compares both the URI and the data type in the Intent object to a URI 577 and data type specified in the filter. The rules are as follows: 578 </p> 579 580 <ol type="a"> 581 <li>An Intent object that contains neither a URI nor a data type passes the 582 test only if the filter likewise does not specify any URIs or data types.</li> 583 584 <li><p>An Intent object that contains a URI but no data type (and a type cannot 585 be inferred from the URI) passes the test only if its URI matches a URI in the 586 filter and the filter likewise does not specify a type. This will be the case 587 only for URIs like {@code mailto:} and {@code tel:} that do not refer to actual data.</p></li> 588 589 <li><p>An Intent object that contains a data type but not a URI passes the test 590 only if the filter lists the same data type and similarly does not specify a URI.</p></li> 591 592 <li><p>An Intent object that contains both a URI and a data type (or a data type 593 can be inferred from the URI) passes the data type part of the test only if its 594 type matches a type listed in the filter. It passes the URI part of the test 595 either if its URI matches a URI in the filter or if it has a {@code content:} 596 or {@code file:} URI and the filter does not specify a URI. In other words, 597 a component is presumed to support {@code content:} and {@code file:} data if 598 its filter lists only a data type.</p></li> 599 </ol> 600 </dl> 601 602 <p> 603 If an intent can pass through the filters of more than one activity or service, 604 the user may be asked which component to activate. An exception is raised if 605 no target can be found. 606 </p> 607 608 609 <h3><a name="ccases"></a>Common cases</h3> 610 611 <p> 612 The last rule shown above for the data test, rule (d), reflects the expectation 613 that components are able to get local data from a file or content provider. 614 Therefore, their filters can list just a data type and do not need to explicitly 615 name the {@code content:} and {@code file:} schemes. 616 This is a typical case. A {@code <data>} element like the following, 617 for example, tells Android that the component can get image data from a content 618 provider and display it: 619 </p> 620 621 <pre><data android:mimeType="image/*" /></pre> 622 623 <p> 624 Since most available data is dispensed by content providers, filters that 625 specify a data type but not a URI are perhaps the most common. 626 </p> 627 628 <p> 629 Another common configuration is filters with a scheme and a data type. For 630 example, a {@code <data>} element like the following tells Android that 631 the component can get video data from the network and display it: 632 </p> 633 634 <pre><data android:scheme="http" android:type="video/*" /></pre> 635 636 <p> 637 Consider, for example, what the browser application does when 638 the user follows a link on a web page. It first tries to display the data 639 (as it could if the link was to an HTML page). If it can't display the data, 640 it puts together an implicit intent with the scheme and data type and tries 641 to start an activity that can do the job. If there are no takers, it asks the 642 download manager to download the data. That puts it under the control 643 of a content provider, so a potentially larger pool of activities 644 (those with filters that just name a data type) can respond. 645 </p> 646 647 <p> 648 Most applications also have a way to start fresh, without a reference 649 to any particular data. Activities that can initiate applications 650 have filters with "{@code android.intent.action.MAIN}" specified as 651 the action. If they are to be represented in the application launcher, 652 they also specify the "{@code android.intent.category.LAUNCHER}" 653 category: 654 </p> 655 656 <pre><intent-filter . . . > 657 <action android:name="android.intent.action.MAIN" /> 658 <category android:name="android.intent.category.LAUNCHER" /> 659 </intent-filter></pre> 660 661 662 <h3><a name="imatch"></a>Using intent matching</h3> 663 664 <p> 665 Intents are matched against intent filters not only to discover a target 666 component to activate, but also to discover something about the set of 667 components on the device. For example, the Android system populates the 668 application launcher, the top-level screen that shows the applications 669 that are available for the user to launch, by finding all the activities 670 with intent filters that specify the "{@code android.intent.action.MAIN}" 671 action and "{@code android.intent.category.LAUNCHER}" category 672 (as illustrated in the previous section). It then displays the icons and 673 labels of those activities in the launcher. Similarly, it discovers the 674 home screen by looking for the activity with 675 "{@code android.intent.category.HOME}" in its filter. 676 </p> 677 678 <p> 679 Your application can use intent matching is a similar way. 680 The {@link android.content.pm.PackageManager} has a set of {@code query...()} 681 methods that return all components that can accept a particular intent, and 682 a similar series of {@code resolve...()} methods that determine the best 683 component to respond to an intent. For example, 684 {@link android.content.pm.PackageManager#queryIntentActivities 685 queryIntentActivities()} returns a list of all activities that can perform 686 the intent passed as an argument, and {@link 687 android.content.pm.PackageManager#queryIntentServices 688 queryIntentServices()} returns a similar list of services. 689 Neither method activates the components; they just list the ones that 690 can respond. There's a similar method, 691 {@link android.content.pm.PackageManager#queryBroadcastReceivers 692 queryBroadcastReceivers()}, for broadcast receivers. 693 </p> 694 695 <h2 id="npex">Note Pad Example</h2> 696 697 <p> 698 The Note Pad sample application enables users to browse through a list 699 of notes, view details about individual items in the list, edit the items, 700 and add a new item to the list. This section looks at the intent filters 701 declared in its manifest file. (If you're working offline in the SDK, you 702 can find all the source files for this sample application, including its 703 manifest file, at {@code <sdk>/samples/NotePad/index.html}. 704 If you're viewing the documentation online, the source files are in the 705 <a href="{@docRoot}resources/samples/index.html">Tutorials and Sample Code</a> 706 section <a href="{@docRoot}resources/samples/NotePad/index.html">here</a>.) 707 </p> 708 709 <p> 710 In its manifest file, the Note Pad application declares three activities, 711 each with at least one intent filter. It also declares a content provider 712 that manages the note data. Here is the manifest file in its entirety: 713 </p> 714 715 <pre><manifest xmlns:android="http://schemas.android.com/apk/res/android" 716 package="com.example.android.notepad"> 717 <application android:icon="@drawable/app_notes" 718 android:label="@string/app_name" > 719 720 <provider android:name="NotePadProvider" 721 android:authorities="com.google.provider.NotePad" /> 722 723 <activity android:name="NotesList" android:label="@string/title_notes_list"> 724 <intent-filter> 725 <action android:name="android.intent.action.MAIN" /> 726 <category android:name="android.intent.category.LAUNCHER" /> 727 </intent-filter> 728 <intent-filter> 729 <action android:name="android.intent.action.VIEW" /> 730 <action android:name="android.intent.action.EDIT" /> 731 <action android:name="android.intent.action.PICK" /> 732 <category android:name="android.intent.category.DEFAULT" /> 733 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 734 </intent-filter> 735 <intent-filter> 736 <action android:name="android.intent.action.GET_CONTENT" /> 737 <category android:name="android.intent.category.DEFAULT" /> 738 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 739 </intent-filter> 740 </activity> 741 742 <activity android:name="NoteEditor" 743 android:theme="@android:style/Theme.Light" 744 android:label="@string/title_note" > 745 <intent-filter android:label="@string/resolve_edit"> 746 <action android:name="android.intent.action.VIEW" /> 747 <action android:name="android.intent.action.EDIT" /> 748 <action android:name="com.android.notepad.action.EDIT_NOTE" /> 749 <category android:name="android.intent.category.DEFAULT" /> 750 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 751 </intent-filter> 752 <intent-filter> 753 <action android:name="android.intent.action.INSERT" /> 754 <category android:name="android.intent.category.DEFAULT" /> 755 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 756 </intent-filter> 757 </activity> 758 759 <activity android:name="TitleEditor" 760 android:label="@string/title_edit_title" 761 android:theme="@android:style/Theme.Dialog"> 762 <intent-filter android:label="@string/resolve_title"> 763 <action android:name="com.android.notepad.action.EDIT_TITLE" /> 764 <category android:name="android.intent.category.DEFAULT" /> 765 <category android:name="android.intent.category.ALTERNATIVE" /> 766 <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> 767 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 768 </intent-filter> 769 </activity> 770 771 </application> 772 </manifest></pre> 773 774 <p> 775 The first activity, NotesList, is 776 distinguished from the other activities by the fact that it operates 777 on a directory of notes (the note list) rather than on a single note. 778 It would generally serve as the initial user interface into the 779 application. It can do three things as described by its three intent 780 filters: 781 </p> 782 783 <ol> 784 <li><pre><intent-filter> 785 <action android:name="android.intent.action.MAIN" /> 786 <category android:name="android.intent.category.LAUNCHER" /> 787 </intent-filter></pre> 788 789 <p> 790 This filter declares the main entry point into the Note Pad application. 791 The standard {@code MAIN} action is an entry point that does not require 792 any other information in the Intent (no data specification, for example), 793 and the {@code LAUNCHER} category says that this entry point should be 794 listed in the application launcher. 795 </p></li> 796 797 <li><pre><intent-filter> 798 <action android:name="android.intent.action.VIEW" /> 799 <action android:name="android.intent.action.EDIT" /> 800 <action android:name="android.intent.action.PICK" /> 801 <category android:name="android.intent.category.DEFAULT" /> 802 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 803 </intent-filter></pre> 804 805 <p> 806 This filter declares the things that the activity can do on a directory 807 of notes. It can allow the user to view or edit the directory (via 808 the {@code VIEW} and {@code EDIT} actions), or to pick a particular note 809 from the directory (via the {@code PICK} action). 810 </p> 811 812 <p> 813 The {@code mimeType} attribute of the 814 <code><a href="{@docRoot}guide/topics/manifest/data-element.html"><data></a></code> 815 element specifies the kind of data that these actions operate on. It 816 indicates that the activity can get a Cursor over zero or more items 817 ({@code vnd.android.cursor.dir}) from a content provider that holds 818 Note Pad data ({@code vnd.google.note}). The Intent object that launches 819 the activity would include a {@code content:} URI specifying the exact 820 data of this type that the activity should open. 821 </p> 822 823 <p> 824 Note also the {@code DEFAULT} category supplied in this filter. It's 825 there because the <code>{@link android.content.Context#startActivity 826 Context.startActivity()}</code> and 827 <code>{@link android.app.Activity#startActivityForResult 828 Activity.startActivityForResult()}</code> methods treat all intents 829 as if they contained the {@code DEFAULT} category — with just 830 two exceptions: 831 </p> 832 833 <ul> 834 <li>Intents that explicitly name the target activity</li> 835 <li>Intents consisting of the {@code MAIN} action and {@code LAUNCHER} 836 category</li> 837 </ul> 838 839 <p> 840 Therefore, the {@code DEFAULT} category is <em>required</em> for all 841 filters — except for those with the {@code MAIN} action and 842 {@code LAUNCHER} category. (Intent filters are not consulted for 843 explicit intents.) 844 </p></li> 845 846 <li><pre><intent-filter> 847 <action android:name="android.intent.action.GET_CONTENT" /> 848 <category android:name="android.intent.category.DEFAULT" /> 849 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 850 </intent-filter></pre> 851 852 <p> 853 This filter describes the activity's ability to return a note selected by 854 the user without requiring any specification of the directory the user should 855 choose from. The {@code GET_CONTENT} action is similar to the {@code PICK} 856 action. In both cases, the activity returns the URI for a note selected by 857 the user. (In each case, it's returned to the activity that called 858 <code>{@link android.app.Activity#startActivityForResult 859 startActivityForResult()}</code> to start the NoteList activity.) Here, 860 however, the caller specifies the type of data desired instead of the 861 directory of data the user will be picking from. 862 </p> 863 864 <p> 865 The data type, <code>vnd.android.cursor.item/vnd.google.note</code>, 866 indicates the type of data the activity can return — a URI for 867 a single note. From the returned URI, the caller can get a Cursor for 868 exactly one item ({@code vnd.android.cursor.item}) from the content 869 provider that holds Note Pad data ({@code vnd.google.note}). 870 </p> 871 872 <p> 873 In other words, for the {@code PICK} action in the previous filter, 874 the data type indicates the type of data the activity could display to the 875 user. For the {@code GET_CONTENT} filter, it indicates the type of data 876 the activity can return to the caller. 877 </p></li> 878 </ol> 879 880 <p> 881 Given these capabilities, the following intents will resolve to the 882 NotesList activity: 883 </p> 884 885 <dl style="margin-left: 2em"> 886 <dt>action: <code>android.intent.action.MAIN</code></dt> 887 <dd>Launches the activity with no data specified.</dd> 888 889 <dt>action: <code>android.intent.action.MAIN</code> 890 <br/>category: <code>android.intent.category.LAUNCHER</code></dt> 891 <dd> Launches the activity with no data selected specified. 892 This is the actual intent used by the Launcher to populate its top-level 893 list. All activities with filters that match this action and category 894 are added to the list.</dd> 895 896 <dt>action: <code>android.intent.action.VIEW</code> 897 <br/>data: <code>content://com.google.provider.NotePad/notes</code></dt> 898 <dd>Asks the activity to display a list of all the notes under 899 <code>content://com.google.provider.NotePad/notes</code>. The user can then 900 browse through the list and get information about the items in it.</dd> 901 902 <dt>action: <code>android.intent.action.PICK</code> 903 <br/>data: <code>content://com.google.provider.NotePad/notes</code></dt> 904 <dd>Asks the activity to display a list of the notes under 905 <code>content://com.google.provider.NotePad/notes</code>. 906 The user can then pick a note from the list, and the activity will return 907 the URI for that item back to the activity that started the NoteList activity.</dd> 908 909 <dt>action: <code>android.intent.action.GET_CONTENT</code> 910 <br/>data type: <code>vnd.android.cursor.item/vnd.google.note</code></dt> 911 <dd>Asks the activity to supply a single item of Note Pad data.</dd> 912 </dl> 913 914 <p> 915 The second activity, NoteEditor, shows 916 users a single note entry and allows them to edit it. It can do two things 917 as described by its two intent filters: 918 919 <ol> 920 <li><pre><intent-filter android:label="@string/resolve_edit"> 921 <action android:name="android.intent.action.VIEW" /> 922 <action android:name="android.intent.action.EDIT" /> 923 <action android:name="com.android.notepad.action.EDIT_NOTE" /> 924 <category android:name="android.intent.category.DEFAULT" /> 925 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 926 </intent-filter></pre> 927 928 <p> 929 The first, primary, purpose of this activity is to enable the user to 930 interact with a single note — to either {@code VIEW} the note or 931 {@code EDIT} it. (The {@code EDIT_NOTE} category is a synonym for 932 {@code EDIT}.) The intent would contain the URI for data matching the 933 MIME type <code>vnd.android.cursor.item/vnd.google.note</code> — 934 that is, the URI for a single, specific note. It would typically be a 935 URI that was returned by the {@code PICK} or {@code GET_CONTENT} 936 actions of the NoteList activity. 937 </p> 938 939 <p> 940 As before, this filter lists the {@code DEFAULT} category so that the 941 activity can be launched by intents that don't explicitly specify the 942 NoteEditor class. 943 </p></li> 944 945 <li><pre><intent-filter> 946 <action android:name="android.intent.action.INSERT" /> 947 <category android:name="android.intent.category.DEFAULT" /> 948 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> 949 </intent-filter></pre> 950 951 <p> 952 The secondary purpose of this activity is to enable the user to create a new 953 note, which it will {@code INSERT} into an existing directory of notes. The 954 intent would contain the URI for data matching the MIME type 955 <code>vnd.android.cursor.dir/vnd.google.note</code> — that 956 is, the URI for the directory where the note should be placed. 957 </p></li> 958 </ol> 959 960 <p> 961 Given these capabilities, the following intents will resolve to the 962 NoteEditor activity: 963 </p> 964 965 <dl style:"margin-left: 2em"> 966 <dt>action: <code>android.intent.action.VIEW</code> 967 <br/>data: <code>content://com.google.provider.NotePad/notes/<var>ID</var></code></dt> 968 <dd>Asks the activity to display the content of the note identified 969 by {@code <var>ID</var>}. (For details on how {@code content:} URIs 970 specify individual members of a group, see 971 <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>.) 972 973 <dt>action: <code>android.intent.action.EDIT</code> 974 <br/>data: <code>content://com.google.provider.NotePad/notes/<var>ID</var></code></dt> 975 <dd>Asks the activity to display the content of the note identified 976 by {@code <var>ID</var>}, and to let the user edit it. If the user 977 saves the changes, the activity updates the data for the note in the 978 content provider.</dd> 979 980 <dt>action: <code>android.intent.action.INSERT</code> 981 <br/>data: <code>content://com.google.provider.NotePad/notes</code></dt> 982 <dd>Asks the activity to create a new, empty note in the notes list at 983 <code>content://com.google.provider.NotePad/notes</code> 984 and allow the user to edit it. If the user saves the note, its URI 985 is returned to the caller. 986 </dd> 987 </dl> 988 989 <p>The last activity, TitleEditor, 990 enables the user to edit the title of a note. This could be implemented 991 by directly invoking the activity (by explicitly setting its component 992 name in the Intent), without using an intent filter. But here we take 993 the opportunity to show how to publish alternative operations on existing 994 data: 995 </p> 996 997 <pre><intent-filter android:label="@string/resolve_title"> 998 <action android:name="com.android.notepad.action.EDIT_TITLE" /> 999 <category android:name="android.intent.category.DEFAULT" /> 1000 <category android:name="android.intent.category.ALTERNATIVE" /> 1001 <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> 1002 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> 1003 </intent-filter></pre> 1004 1005 <p> 1006 The single intent filter for this activity uses a custom action called 1007 "<code>com.android.notepad.action.EDIT_TITLE</code>". It must be invoked on 1008 a specific note (data type <code>vnd.android.cursor.item/vnd.google.note</code>), 1009 like the previous {@code VIEW} and {@code EDIT} actions. However, here the 1010 activity displays the title contained in the note data, not the content of 1011 the note itself. 1012 </p> 1013 1014 <p> 1015 In addition to supporting the usual {@code DEFAULT} category, the title 1016 editor also supports two other standard categories: 1017 <code>{@link android.content.Intent#CATEGORY_ALTERNATIVE ALTERNATIVE}</code> 1018 and <code>{@link android.content.Intent#CATEGORY_SELECTED_ALTERNATIVE 1019 SELECTED_ALTERNATIVE}</code>. 1020 These categories identify activities that can be presented to users in 1021 a menu of options (much as the {@code LAUNCHER} category identifies 1022 activities that should be presented to user in the application launcher). 1023 Note that the filter also supplies an explicit label (via 1024 <code>android:label="@string/resolve_title"</code>) to better control 1025 what users see when presented with this activity as an alternative 1026 action to the data they are currently viewing. (For more information 1027 on these categories and building options menus, see the 1028 <code>{@link android.content.pm.PackageManager#queryIntentActivityOptions 1029 PackageManager.queryIntentActivityOptions()}</code> and 1030 <code>{@link android.view.Menu#addIntentOptions Menu.addIntentOptions()}</code> 1031 methods.) 1032 </p> 1033 1034 <p> 1035 Given these capabilities, the following intent will resolve to the 1036 TitleEditor activity: 1037 </p> 1038 1039 <dl style="margin-left: 2em"> 1040 <dt>action: <code>com.android.notepad.action.EDIT_TITLE</code> 1041 <br/>data: <code>content://com.google.provider.NotePad/notes/<var>ID</var></code></dt> 1042 <dd>Asks the activity to display the title associated with note <var>ID</var>, and 1043 allow the user to edit the title.</dd> 1044 </dl> 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056