Deep linking in Android — Part 1

Gagan Batra
AndroidPub
Published in
3 min readMar 15, 2018
Image Courtesy- https://wall.alphacoders.com/big.php?i=665092

You: Hey Gagan, I know jack squat about deep linking. Can you please help me out ?
Me: Want to open your app on clicking a URL ? Yes, with deep linking it’s possible. You don’t have to touch the app icon from home screen of your mobile. The app will automatically be launched on clicking a URL. It also allows you to pass the data from URL to app(also known as query params).

As per the official documentation on android developer’s page:

When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds:

- Open the user’s preferred app that can handle the URI, if one is designated

- Open the only available app that can handle the URI

- Allow the user to select an app from a dialog

We need to add intent filters in our app to handle incoming links with the following attributes and elements:
<action>, <data> and <category>. These will let the system know our app can also handle particular type URL’s.

You : Wait ! What ? Particular type of URL’s ? What do you mean by that ?
Me: I meant you will have to mention the scheme, host and pathPrefix in your <data> tag. See the example below:

<intent-filter>
<action android:name=”android.intent.action.VIEW” />
<category android:name=”android.intent.category.DEFAULT” />
<category android:name=”android.intent.category.BROWSABLE” />
<data android:scheme=”http”
android:host=”www.example.com"
android:pathPrefix=”/gizmos” />
</intent-filter>

Above example will only handle http://www.example.com/gizmos

You: Well, this looks easy. Is there a way to handle multiple URL’s from the same app ?
Me: Yes, you can do that. You can also add multiple <intent -filter> tags to handle multiple URL’s. You can also add multiple <data> tags in your <intent-filter>(See below example) but it is considered a good practice to mention separate <intent-filter> tags as it enhances readability and clearly explains the intent behind it.

<intent-filter>
<action android:name=”android.intent.action.VIEW” />
<category android:name=”android.intent.category.DEFAULT” />
<category android:name=”android.intent.category.BROWSABLE” />
<data android:scheme=”http”
android:host=”www.example.com"
android:pathPrefix=”/gizmos” />
<data android:scheme=”http”
android:host=”www.example.com"
android:pathPrefix=”/foobar” />
</intent-filter>

It will now handle http://www.example.com/gizmos and http://www.example.com/foobar

You: Yeah ! I am starting to feel a little confident about it. Wait, you also mentioned something about passing data from URL to app. How about that ?
Me: Yeah right, we can do that as well. Below is the sample URL with dummy data passed along with it:

http://www.example.com/gizmos?data=12345

Now we need to handle it in our activity’s onCreate() method. Just add these lines after setContentView()

Intent intent = getIntent();
Uri data = intent.getData();

You: Thanks a lot Gagan. I feel a lot better now.

Me: Bonus tip- You can have custom schemes as well like abc in place of http or https. See below example for this URL- abc://www.example.com/gizmos

<intent-filter>
<action android:name=”android.intent.action.VIEW” />
<category android:name=”android.intent.category.DEFAULT” />
<category android:name=”android.intent.category.BROWSABLE” />
<data android:scheme=”abc”
android:host=”www.example.com"
android:pathPrefix=”/gizmos” />
</intent-filter>

That’s all. I tried to make this tutorial a little interesting. Hit applaud if you found it helpful.

Cheers !!!

More tutorials in the series:

  1. Deep Linking in Android — Part 1 [Current]
  2. Deep Linking in Android — Part 2

I have created an audio recorder application in Android and I welcome everyone who is reading this post to contribute in any way you can. There is a lot of scope for improvement in this project for both Developers and QA Engineers. You can start by downloading the apk from play store and start exploring. The link is given below:

Playstore https://play.google.com/store/apps/details?id=com.odio.adfree

Sourcehttps://github.com/gbatra24/Odio

Don’t forget to check out my other posts. To read them, click here.

--

--