Select Page

This article discusses what deep linking is, why we use deep linking, how to enable and test deep linking in your android app.

What is deep linking ?

Deep linking provides a way to link to specific content on either a website or an application. It allows a corresponding Android app to open instead of a web browser. It can also act on any information that is contained in a particular URL or linked text. This is where deep linking starts to get really useful.

Why we use deep linking ?

Deep linking is important because it drives user engagement. Deep links can direct users to specific views within your app or other apps. It breaks the back button. For example, As you browse, you click a link to a URL at Facebook.com. By the power of deep link, this opens in the Facebook app. Without deep linking, you have two options.First , you may need to authenticate with Facebook if you’re not already signed in. The second option is exiting  your app/browser and opening the facebook app where you will have to search for the same content again.

deep linking

You can use this for your referral campaigns, content sharing, and in-app referrals to the services, users, and campaigns that helped users find and use your app.

How to enable deep linking ?

To enable deep linking into your application you need to use the below snippet:

1. Add intent filters within the <activity> tag in Android Manifest :


<activity android:name=".DeepLinking.DeeplinkActivity">
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- URI http://www.droidmentor.com -->
        <data
            android:host="www.droidmentor.com"
            android:scheme="http" />
        <!-- URI http://droidmentor.com -->
        <data
            android:host="droidmentor.com"
            android:scheme="http" />
    </intent-filter>
</activity>

intent – filter  contains the below elements and attribute values

  • autoVerify  : The system treats your app as the default handler for the specified URI pattern. If it successfully verifies all app link patterns declared in your manifest.
  • <action> :  Sets the intent action to view the activity.
  • <category> :  Browsable : Allows the link to be opened from a web browser. Default : Allows the deep link to be used without specifying the app name.
  • <data> : represents a URI format that relates to the activity.
    • scheme : This is the minimal essential attribute for specifying a URI. A scheme is specified without the trailing colon (for example, http ). Scheme matching in the Android framework is case-sensitive.
    • host : The host part of a URI authority.
    • port : The port part of a URI authority.
    • path : It must begin with a “/“. It specifies a complete path that is matched against the complete path in an Intent object.
    • pathPrefix : the partial path that is matched against only the initial part of the path.
    • pathPattern : It specifies a complete path that is matched against the complete path.
    • mimeType : A MIME media type, such as image/jpeg or audio/mpeg4-generic.

2. Read data from incoming intents :


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_deeplink);

    Intent intent=getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();
    if (data != null) {
        String scheme = data.getScheme(); // "http"
        String host = data.getHost(); // "droidmentor.com"
        Log.d(TAG, "scheme: "+scheme);
        Log.d(TAG, "host: "+host);
    }
}

Once the system starts your activity through an intent filter, you can use data provided by the Intent to determine what you need to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent.

How to test your deep links ?

After enabling this deep linking, you need to test whether your code works properly or not. For testing your deep links you need to 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 > < YOUR PACKAGE NAME >

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 "http://droidmentor.com" droidmentor.deeplinking

Now that you know how to create and use deep links.

Happy deep linking 🙂

If this post helped you, please help others by sharing this post, keep watching this space get more updates on Android Stuff!

 

Elsewhere

Jaison Fernando

Android Developer at NFN Labs
Elsewhere