Deep Linking is a methodology for launching a native mobile application via a link. It consists of a unique URI (Uniform Resource Identifier) that links or matches to a specific location within a mobile app.
For example “https://www.cuelogic.com/registration?cd=23424” is a url that contains a key ‘cd’ with a value 23424 which is a user registration id. You can process this url and retrieve data from it.
Basically, all Android apps are composed of a set of Activities. Each of these Activities can be called by other apps if configured properly in AndroidManifest.xml file. Depending on your requirement you can launch the specific Activity once a user clicks on the deep link.
It is important to note that once a user clicks on a deep link, the Android Activity will launch on top of the current visible Activity (In case if app is open). So it is your responsibility to ensure the appropriate view hierarchy of the application. Also when the app is opened through deep link, you can retrieve the URL and process it according to your needs.
Here is a quick demo of Deep Linking implementation in Android application.
To make a clickable link in web browser we would require a html file. Create a file named index.html and put the below code in it, you can store this file on your SD card and open it using a web browser like Google Chrome.
<html> <head> <title>www.cuelogic.com</title> </head> <body> Registration Link : https://www.cuelogic.com/registration?cd=23424″ Register Here </body> </html>
The index.html preview will look something like this.
Changes need to be done in AndroidManifest.xml file:
<?xml version=”1.0″ encoding=”utf-8″?> <manifest xmlns_android=”http://schemas.android.com/apk/res/android” package=”YOUR_PACKAGE_NAME”> <application android_allowBackup=”true” android_icon=”@mipmap/app_icon” android_label=”@string/app_name” android_theme=”@style/AppTheme”> <activity android_name=”.DeepLinkingActivity” android_label=”@string/app_name” android_launchMode=”singleTask”> <!– Note: android_launchMode=”singleTask” The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one. –> <intent-filter> <!– Note: android_name=”android.intent.action.VIEW” To make intent filter can be reached from Google Search. –> <action android_name=”android.intent.action.VIEW” /> <!– Note: android_name=”android.intent.category.DEFAULT” This is optional category, but recommended. Without this category, the activity can be started only with an explicit intent, using your app component name. –> <category android_name=”android.intent.category.DEFAULT” /> <!– Note: android_name=”android.intent.category.BROWSABLE” To safely invoke this Activity from a browser when a user clicks on a hyperlink. –> <category android_name=”android.intent.category.BROWSABLE” /> <!– Note: The <data> tag used to represent a URI format that resolves to the activity. You can add additional attributes to further refine the type of URI that the activity accepts. For example like android_host=”34.200.212.66”, android_scheme=”https” and android_pathPrefix=”/registration” –> <data android_host=”34.200.212.66″ android_pathPrefix=”/registration” android_scheme=”https” /> </intent-filter> </activity> </application> </manifest> To process the data received from deep linking you need to write the Activity class.
Create a DeepLinkingActivity.java class and put the code displayed below in it. package YOUR_PACKAGE_NAME; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; /////////////////////////////////////////////////////////////////////////// // Used to demonstrate the Deep linking functionality in Android app. /////////////////////////////////////////////////////////////////////////// public class DeepLinkingActivity extends AppCompatActivity { public static final String KEY_CD = “cd”; private Uri uriData; private TextView dataTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_deep_linking); dataTextView = (TextView) findViewById(R.id.dataTextViewId); } @Override protected void onResume() { super.onResume(); Intent intent = getIntent(); if(intent != null) { uriData = intent.getData(); if (uriData != null) { //To get scheme. String scheme = uriData.getScheme(); //To get server name. String host = uriData.getHost(); //To get parameter value from the URI. String parameterValue = uriData.getQueryParameter(KEY_CD); StringBuilder builder = new StringBuilder(); builder.append(“Data Received From URI:n”+uriData.toString()+”n”); builder.append(“——————————————–“+”n”); builder.append(“Scheme: “+scheme+”n”); builder.append(“——————————————–“+”n”); builder.append(“Host: “+host+”n”); builder.append(“——————————————–“+”n”); builder.append(“CD key value: “+parameterValue+”n”); builder.append(“——————————————–“+”n”); // Set the retrieved value to dataTextView. dataTextView.setText(builder.toString()); } } } /** * To get the latest intent object. * @param intent */ @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); } }
Now run the Android application. Once the app runs successfully, open the index.html file in your favorite browser and click on the “Register Here” link, Android system will show you the list of apps that can open the link. This list will have your application as well, as shown in the image:
Select your application, this will launch your app and process the data received from deep link, as shown in the image: