Home | History | Annotate | Download | only in app-indexing
      1 page.title=Enabling Deep Links for App Content
      2 trainingnavtop=true
      3 
      4 @jd:body
      5 
      6 <!-- This is the training bar -->
      7 <div id="tb-wrapper">
      8 <div id="tb">
      9 
     10 <h2>This lesson teaches you to</h2>
     11 <ol>
     12   <li><a href="#adding-filters">Add Intent Filters for Your Deep Links</a></li>
     13   <li><a href="#handling-intents">Read Data from Incoming Intents</a></li>
     14   <li><a href="#testing-filters">Test Your Deep Links</a></li>
     15 </ol>
     16 
     17 <h2>You should also read</h2>
     18 <ul>
     19 <li><a href="{@docRoot}guide/components/intents-filters.html">Intents and Intent Filters</a></li>
     20 <li><a href="{@docRoot}training/basics/intents/filters.html">Allow Other Apps to Start Your Activity</a></li>
     21 </ul>
     22 
     23 </div>
     24 </div>
     25 
     26 <p>To enable Google to crawl your app content and allow users to enter your app
     27   from search results, you must add intent filters for the relevant
     28   activities in your app manifest. These intent filters allow
     29   <em>deep linking</em> to the content in any of your activities. For example, the user might click on a deep link to view a page within a shopping app that describes a product offering that the user is searching for.</p>
     30 
     31 <h2 id="adding-filters">Add Intent Filters for Your Deep Links</h2>
     32 <p>To create a deep link to your app content, add an intent filter that
     33   contains these elements and attribute values in your manifest:</p>
     34 <dl>
     35 <dt><a href="{@docRoot}guide/topics/manifest/action-element.html">{@code &lt;action&gt;}</a></dt>
     36 <dd>Specify the {@link android.content.Intent#ACTION_VIEW} intent action so
     37   that the intent filter can be reached from Google Search.</dd>
     38 <dt><a href="{@docRoot}guide/topics/manifest/data-element.html">{@code &lt;data&gt;}</a></dt>
     39 <dd>Add one or more <a href="{@docRoot}guide/topics/manifest/data-element.html">{@code &lt;data&gt;}</a> tags, where each tag represents a URI format that resolves to the activity. At minimum, the <a href="{@docRoot}guide/topics/manifest/data-element.html">{@code &lt;data&gt;}</a> tag must include the <a href="{@docRoot}guide/topics/manifest/data-element.html#scheme">{@code android:scheme}</a> attribute.
     40 <p>You can add additional attributes to further refine the type of URI that the activity accepts. For example, you might have multiple activities that accept similar URIs, but which differ simply based on the path name. In this case, use the <a href="{@docRoot}guide/topics/manifest/data-element.html#path">{@code android:path}</a> attribute or its variants ({@code pathPattern} or {@code pathPrefix}) to differentiate which activity the system should open for different URI paths.</p></dd>
     41 <dt><a href="{@docRoot}guide/topics/manifest/category-element.html">{@code &lt;category&gt;}</a></dt>
     42 <dd>Include the {@link android.content.Intent#CATEGORY_BROWSABLE BROWSABLE}
     43 category. The {@link android.content.Intent#CATEGORY_BROWSABLE BROWSABLE}
     44 category is required in order for the intent filter to be accessible from a web
     45 browser. Without it, clicking a link in a browser cannot resolve to your app.
     46 The {@link android.content.Intent#CATEGORY_DEFAULT DEFAULT} category is
     47 optional, but recommended. Without this category, the activity can be started
     48 only with an explicit intent, using your app component name.
     49 </dd>
     50 </dl>
     51 
     52 <p>The following XML snippet shows how you might specify an intent filter
     53 in your manifest for deep linking. The URIs {@code example://gizmos} and
     54 {@code http://www.example.com/gizmos} both resolve to this activity.</p>
     55 
     56 <pre>
     57 &lt;activity
     58     android:name="com.example.android.GizmosActivity"
     59     android:label="@string/title_gizmos" &gt;
     60     &lt;intent-filter android:label="@string/filter_title_viewgizmos"&gt;
     61         &lt;action android:name="android.intent.action.VIEW" /&gt;
     62         &lt;category android:name="android.intent.category.DEFAULT" /&gt;
     63         &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
     64         &lt;!-- Accepts URIs that begin with "example://gizmos --&gt;
     65         &lt;data android:scheme="example"
     66               android:host="gizmos" /&gt;
     67         &lt;!-- Accepts URIs that begin with "http://www.example.com/gizmos --&gt;
     68         &lt;data android:scheme="http"
     69               android:host="www.example.com"
     70               android:pathPrefix="gizmos" /&gt;
     71     &lt;/intent-filter&gt;
     72 &lt;/activity&gt;
     73 </pre>
     74 
     75 <p>Once you've added intent filters with URIs for activity content to your app
     76 manifest, Android is able to route any {@link android.content.Intent}
     77 that has matching URIs to your app at runtime.</p>
     78 
     79 <p>To learn more about defining intent filters, see <a href="{@docRoot}training/basics/intents/filters.html">Allow Other Apps to Start Your Activity</a>.</p>
     80 
     81 <h2 id="handling-intents">Read Data from Incoming Intents</h2>
     82 <p>Once the system starts your activity through an intent filter, you can
     83   use data provided by the {@link android.content.Intent} to determine what you need to render. Call the {@link android.content.Intent#getData()} and
     84 {@link android.content.Intent#getAction()} methods to retrieve the data and
     85 action associated with the incoming {@link android.content.Intent}. You can
     86 call these methods at any time during the lifecycle of the activity, but you
     87 should generally do so during early callbacks such as {@link
     88 android.app.Activity#onCreate(android.os.Bundle) onCreate()} or
     89 {@link android.app.Activity#onStart()}.</p>
     90 <p>Heres a snippet that shows how to retrieve data from an
     91 {@link android.content.Intent}:</p>
     92 <pre>
     93 &#64;Override
     94 public void onCreate(Bundle savedInstanceState) {
     95     super.onCreate(savedInstanceState);
     96     setContentView(R.layout.main);
     97 
     98     Intent intent = getIntent();
     99     String action = intent.getAction();
    100     Uri data = intent.getData();
    101 }
    102 </pre>
    103 <p>Follow these best practices to improve the user's experience:</p>
    104 <ul>
    105 <li>The deep link should take users directly to the content,
    106 without any prompts, interstitial pages, or logins. Make sure that users can
    107 see the app content even if they never previously opened the application.
    108 It is okay to prompt users on subsequent interactions or when they open the app
    109 from the Launcher. This is the same principle as the <a href="https://support.google.com/webmasters/answer/74536?hl=en" class="external-link" target="_blank">first click free</a> experience for web sites.</li>
    110 <li>Follow the design guidance described in
    111   <a href="{@docRoot}design/patterns/navigation.html">Navigation with Back and Up</a>
    112   so that your app matches users' expectations for backward navigation after
    113   they enter your app through a deep link.
    114 </li>
    115 </ul>
    116 
    117 <h2 id="testing-filters">Test Your Deep Links</h2>
    118 <p>You can use the <a href="{@docRoot}tools/help/adb.html">Android Debug
    119 Bridge</a> with the activity manager (am) tool to test that the intent filter
    120 URIs you specified for deep linking resolve to the correct app activity. You
    121 can run the adb command against a device or an emulator.</p>
    122 <p>The general syntax for testing an intent filter URI with adb is:</p>
    123 <pre>
    124 $ adb shell am start
    125         -W -a android.intent.action.VIEW
    126         -d &lt;URI&gt; &lt;PACKAGE&gt;
    127 </pre>
    128 <p>For example, the command below tries to view a target app activity that
    129 is associated with the specified URI.</p>
    130 <pre>
    131 $ adb shell am start
    132         -W -a android.intent.action.VIEW
    133         -d "example://gizmos" com.example.android
    134 </pre>
    135