How to enable deep links in Android

Every year thousands of developers and programmers step into Android market. According to the needs this tutorial will explain how to enable deep links in Android. Before explaining it here is a brief introduction of deep links.

What are Deep Links?

Deep link is a very important concept while developing an application whether it is a mobile application or a web application. In the context of web, deep links are hyperlinks that takes you to the specified content other than the home page of a web application.

While mobile deep links are URIs that link to a specified location within a mobile app. The term “deep” refers to the application’s hierarchical structure of pages. Example of deep link is consider an online shopping app in your mobile, you see a dress and you send the link of that dress to your friend, when your friend opens the link, it will take him directly to pay in the app.

The link you used is an example of deep link. Without use of this deep link your friend would have to find that app on play store, install it and then search for that pen in the app.

Google uses deep links to crawl your app content and allow users to enter your app from search results. Deep links are used to create notifications that open a specific page when clicked.

Implementing Deep Links in Android

Adding deep links in your Android app is very simple. For implementing deep links you need Android Studio, a mobile app code, an Android test device and a USB cable.

Follow three simple and basic steps.

Intent filter
To add a deep link in your Android app, go to the Android Studio, open your application’s manifest file and add deep link as an intent filter to the relevant activities.

Elements and attributes of intent filter are
<action>
Action tag defines what happens in the app when a link is clicked. It specifies the action view.
<category>
Category tag defines the properties of deep link. BROWSABLE category allows the link to be opened from a web browser. DEFAULT category allows the deep link to be opened without specifying the app name.
<data>
The data tag specifies URI as a deep link. Data tag must include android:scheme attribute.
Here is the manifest sample code in XML form of my app which calculates GPA.

<?xml version = "1.0" encoding = "utf-8" ?>
<?xml version = "1.0" encoding = "utf-8" ?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
    package = "smartgpa.calculator.admin.smartGpaClasses" >

    <uses-permission android:name = "android.permission.INTERNET" />

    <application
        android:allowBackup = "true"
        android:icon = "@mipmap/ic_launcher"
        android:label = "@string/app_name"
        android:supportsRtl = "true"
        android:theme = "@style/AppTheme" >
        <activity android:name = ".LoginActivity" >
            <intent-filter>
                <action android:name = "android.intent.action.MAIN" />

                <category android:name = "android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name = ".SemesterViewActivity" >
        <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 example://deeplink -->
            <data android:scheme = "example" android:host = "deeplink"/>
            <!-- URI http://www.mydeeplink.com/semester -->
            <data android:scheme = "http" android:host = "www.mydeeplink.com" android:pathPrefix = "/semester" />
        </intent-filter>
        </activity>

        <activity android:name = ".semester_gpa" />
        <activity android:name = ".SignUp" />
        <activity android:name = ".Semester_CGPA" />
    </application>

</manifest>

In this example navigating to mydeeplink.com will take you to the SemesterView activity. For each URI scheme you need a separate intent filter.
Note: If you have multiple deep link formats you will need to add an individual intent filter to handle each one.

Handling deep links

Now you need to add code in your app to show that particular resource when a deep link is clicked. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming intent. You can call these methods at any time during the life cycle of your activity but it would be better to add these operations in OnCreate() or OnStart().
The following code shows how to retrieve data from an intent

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

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

Add your code below getData. Deep link should take user directly to the activity without prompting or logins.

Testing Deep Link

There are many different and easy ways to test a deep link. You can test it with Android Studio or you can use Android Debug Bridge. I will explain with both methods.
You can test deep link on your app installed in your phone. Also you can test your app’s behavior using html page in a browser, for this you have to create an html page with an intent link in it.

Using Android Debug Bridge, open command line terminal in your computer and try to trigger it like this

Here is the general syntax

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

For the example code
./adb shell am start
-W -a android.intent.action.VIEW \
-d “example:semester” com.myapp.activity.SemesterViewActivity

You can directly test it with Android Studio

Click Run -> Edit Configurations
Go to the General tab in the top
In the Launch Options, enter the URI in the Deep Link field
When you run your app using Android Studio it will open the specified URI.

In conclusion

Deep Links have multiple uses, you can add app deep links on your site. Deep links are used to improve Google’s crawling on your app. Remember every hyperlink is not a deep link. Advantage of deep link is it takes user directly to the specified resource without any extra logins or prompting. Deep links improve user efficiency and reduce the likelihood of users’ deviating from their tasks. It is an effective way of delivering content directly to the users.

 

Add a Comment