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