page.title=Enabling Deep Links for App Content trainingnavtop=true @jd:body

This lesson teaches you to

  1. Add Intent Filters for Your Deep Links
  2. Read Data from Incoming Intents
  3. Test Your Deep Links

You should also read

To enable Google to crawl your app content and allow users to enter your app from search results, you must add intent filters for the relevant activities in your app manifest. These intent filters allow deep linking 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.

Add Intent Filters for Your Deep Links

To create a deep link to your app content, add an intent filter that contains these elements and attribute values in your manifest:

{@code <action>}
Specify the {@link android.content.Intent#ACTION_VIEW} intent action so that the intent filter can be reached from Google Search.
{@code <data>}
Add one or more {@code <data>} tags, where each tag represents a URI format that resolves to the activity. At minimum, the {@code <data>} tag must include the {@code android:scheme} attribute.

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 {@code android:path} attribute or its variants ({@code pathPattern} or {@code pathPrefix}) to differentiate which activity the system should open for different URI paths.

{@code <category>}
Include the {@link android.content.Intent#CATEGORY_BROWSABLE BROWSABLE} category. The {@link android.content.Intent#CATEGORY_BROWSABLE BROWSABLE} category is required in order for the intent filter to be accessible from a web browser. Without it, clicking a link in a browser cannot resolve to your app. The {@link android.content.Intent#CATEGORY_DEFAULT DEFAULT} category is optional, but recommended. Without this category, the activity can be started only with an explicit intent, using your app component name.

The following XML snippet shows how you might specify an intent filter in your manifest for deep linking. The URIs {@code “example://gizmos”} and {@code “http://www.example.com/gizmos”} both resolve to this activity.

<activity
    android:name="com.example.android.GizmosActivity"
    android:label="@string/title_gizmos" >
    <intent-filter android:label="@string/filter_title_viewgizmos">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
        <data android:scheme="http"
              android:host="www.example.com"
              android:pathPrefix="/gizmos" />
        <!-- note that the leading "/" is required for pathPrefix-->
        <!-- Accepts URIs that begin with "example://gizmos”
        <data android:scheme="example"
              android:host="gizmos" />
        -->
    </intent-filter>
</activity>

Once you've added intent filters with URIs for activity content to your app manifest, Android is able to route any {@link android.content.Intent} that has matching URIs to your app at runtime.

Note: Intent filters may only contain a single {@code data} element for a URI pattern. Create separate intent filters to capture additional URI patterns.

To learn more about defining intent filters, see Allow Other Apps to Start Your Activity.

Read Data from Incoming Intents

Once the system starts your activity through an intent filter, you can use data provided by the {@link android.content.Intent} to determine what you need to render. Call the {@link android.content.Intent#getData()} and {@link android.content.Intent#getAction()} methods to retrieve the data and action associated with the incoming {@link android.content.Intent}. You can call these methods at any time during the lifecycle of the activity, but you should generally do so during early callbacks such as {@link android.app.Activity#onCreate(android.os.Bundle) onCreate()} or {@link android.app.Activity#onStart()}.

Here’s a snippet that shows how to retrieve data from an {@link android.content.Intent}:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();
}

Follow these best practices to improve the user's experience:

Test Your Deep Links

You can use the Android Debug Bridge with the activity manager (am) tool to test that the intent filter URIs you specified for deep linking resolve to the correct app activity. You can run the adb command against a device or an emulator.

The general syntax for testing an intent filter URI with adb is:

$ adb shell am start
        -W -a android.intent.action.VIEW
        -d <URI> <PACKAGE>

For example, the command below tries to view a target app activity that is associated with the specified URI.

$ adb shell am start
        -W -a android.intent.action.VIEW
        -d "example://gizmos" com.example.android