Implementing Deep Links in Android Apps

  • Deep links are links which open a specific page within an app.
  • Optionally, some data or parameter can be passed along.
  • Also, deep links allow Google to index your app and link to specific sections of your app in searches. The deep link appears as a search result in Google and takes the user to a particular page of your app.

Adding a deep link

To add a deep link to a particular activity in your app, open AndroidManifest.xml and in the particular activity’s tag, add these intent filters

<activity 
    android:name="com.your.app.LinkActivity" 
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    
    <intent-filter>
            <!-- Sets the intent action to view the activity -->
            <action android:name="android.intent.action.VIEW" />
            <!-- Allows the link to be opened from a web browser -->
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Allows the deep link to be used without specifying the app name -->
            <category android:name="android.intent.category.DEFAULT" />
            <!-- URI myapp://somelink -->
            <data android:scheme="myapp" android:host="somelink"/>
            <!-- URI http://mysite.com -->
            <data android:scheme="http" android:host="mysite.com" android:pathPrefix="/products"/>
    </intent-filter>
</activity>

To differentiate different links in your activity, or to get the optional query parameter in your activity:

int item_id = getIntent().getIntExtra("item_id",0);  //if value passed from intent

//if recieved from deep link http://mysite.com/products?item_id=7
final Uri uri = getIntent().getData();
if (uri != null) {
    if ("http".equals(uri.getScheme()) && "mysite.com".equals(uri.getHost())) {
        try { item_id = Integer.parseInt(uri.getQueryParameter("item_id")); } catch (Exception e) { }
    }
}

Android Studio provides a tool for testing the links, check the Step 4 of the next section.

Recommended method: Using App Links Assistant in Android Studio

Android Studio provides a tool that makes implementation of deep links much easier and also enables testing it.

Under Android Studio Tools Menu, select App Links Assistant. You will see it has 4 very distinct steps for implementing and testing deep links.

Step 1: Add URL intent filters

Click the add button,

  • specify your URL, example http://mysite.com
  • specify path prefix, example /products
  • select your activity from the drop-down list
  • and click OK.
  • done! Check your Manifest file, App Links Assistant will already have added the necessary lines.

Step 2: Add logic to handle the intent

This is even simpler. Just select the activity and click on “Insert Code” button. The assistant will automatically add the code to get the URI data in your activity.

Modify it to get specific values (as shown in 1st method above), and handle them as you like.

Step 3: Associate Website

This step is important if you want the link to open the app directly without prompting the user to choose browser or app. This step basically generates a JSON file, called Digital Assets Link (DAL) file, which has to be uploaded to your server.

Most of the fields will already be populated in the editor, just pay attention to the Fingerprint of the signing certificate, and generate the file. The assistant will tell you the exact location to place the file. Do not change the name or location.

At the bottom, you check if you completed this correctly via the Link and Verify’ button.

Step 4: Test your deep link.

The final step is just to test if everything runs as expected.

Enter a link, example http://mysite.com/products?item_id=7; and run on your emulator and see the result.

 


Also published on Medium.

By |2019-04-03T03:57:22+00:00March 14th, 2019|Categories: Android Tutorials|0 Comments

Leave A Comment