Deep Linking in Android with Example - GeeksforGeeks

04 Feb, 2021
Deep Linking is one of the most important features that is used by various apps to gather data inside their apps in the form of a URL link. So it becomes helpful for the users from other apps to easily share the data with different apps. In this article, we will take a look at the implementation of deep links in our Android App. 
A deep link is a URL link that is generated, when anyone clicks on that link our app will be open with a specific activity or a screen. Using this URL we can send a message to our app with parameters. In WhatsApp, we can generate a deep link to send a message to a phone number with some message in it. Deep links are used to open your app’s specific screen with a URL link.   
We will be building a simple application in which we will be creating a deep link and on clicking on that link we will be passing our message to our app and display that message in a text view. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:weightSum="5"    tools:context=".MainActivity">      <!--text view for displaying welcome message-->    <TextView        android:id="@+id/idTVWelcome"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:padding="10dp"        android:text="Welcome to "        android:textAlignment="center"        android:textAllCaps="false"        android:textColor="@color/purple_500"        android:textSize="30sp" />      <!--text view for displaying the organization        name from the link which we have generated-->    <TextView        android:id="@+id/idTVMessage"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/idTVWelcome"        android:text="Organization Name"        android:textAlignment="center"        android:textAllCaps="false"        android:textColor="@color/purple_500"        android:textSize="30sp"        android:textStyle="bold" />      </RelativeLayout>
Step 3: Working with the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml and add the below code to it. As we are creating a deep link for our MainActivity.java file so we have to add this code in the MainActivity part. Below is the code which is to be added in the AndroidManifext.xml file. Comments are added in the code to get to know in more detail. 
XML
<!--as we want to open main activity from our link so we are specifying    only in main activity or we can specify that in different activity as well    on below line we are adding intent filter to our MainActivity--><intent-filter>  <!--below line is to set the action to our intent to view-->  <action android:name="android.intent.action.VIEW" />      <!--on below line we are adding a default category to our intent-->  <category android:name="android.intent.category.DEFAULT" />      <!--on below line we are adding a category to make our app browsable-->  <category android:name="android.intent.category.BROWSABLE" />      <!--on below line we are specifying the host name and      the scheme type from which we will be calling our app-->   <data        android:host="www.chaitanyamunje.com"        android:scheme="https" /></intent-filter>  <!--below is the same filter as above just the scheme is changed to http --><!--so we can open our app with the url starting with https and http as well--><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:host="www.chaitanyamunje.com"    android:scheme="http" /></intent-filter>
Below is the complete code for the AndroidManifest.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.deeplinks">      <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/Theme.DeepLinks">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>              <!--as we want to open main activity from our link so we are specifying                only in main activity or we can specify that in different activity as well-->            <!--on below line we are adding intent filter to our MainActivity-->            <intent-filter>                <!--below line is to set the action to our intent to view-->                <action android:name="android.intent.action.VIEW" />                <!--on below line we are adding a default category to our intent-->                <category android:name="android.intent.category.DEFAULT" />                <!--on below line we are adding a category to make our app browsable-->                <category android:name="android.intent.category.BROWSABLE" />                <!--on below line we are specifying the host name and                    the scheme type from which we will be calling our app-->                <data                    android:host="www.chaitanyamunje.com"                    android:scheme="https" />            </intent-filter>              <!--below is the same filter as above just the scheme is changed to http -->            <!--so we can open our app with the url starting with https and http as well-->            <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:host="www.chaitanyamunje.com"                    android:scheme="http" />            </intent-filter>          </activity>    </application></manifest>
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
package com.example.deeplinks;  import android.net.Uri;import android.os.Bundle;import android.widget.TextView;  import androidx.appcompat.app.AppCompatActivity;  import java.util.List;  public class MainActivity extends AppCompatActivity {      // creating a variable for our text view    private TextView messageTV;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                  // initializing our variable        messageTV = findViewById(R.id.idTVMessage);                  // getting the data from our        // intent in our uri.        Uri uri = getIntent().getData();                  // checking if the uri is null or not.        if (uri != null) {            // if the uri is not null then we are getting the             // path segments and storing it in list.            List<String> parameters = uri.getPathSegments();                          // after that we are extracting string from that parameters.            String param = parameters.get(parameters.size() - 1);                          // on below line we are setting            // that string to our text view            // which we got as params.            messageTV.setText(param);        }    }}
Now we have added the URL in our AndroidManifest file as https://www.chaitanyamunje.com/hello/GeeksForGeeks. The URL from which we will send messages to our MainActivity.java file. In the above URL the “https” is our scheme, “www.chaitanyamunje.com” is our hostname and “hello” is our first parameter and “GeeksForGeeks” is a second parameter which we are going to show in our app as an organization name. You can change your parameters according to your requirement. Now run your app and see the output of the app.
As you have run your app you will get to see the text as Organization name, now close the application and click on the link which is shown above from the device in which your application is installed. After clicking on that link a pop-up message will be displayed to select the application. Inside that pop-up message select, your application, and your app will be open. We are passing a message as “GeeksForGeeks” which will be displayed in the place of the Organization name.
android
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Resource Raw Folder in Android Studio
CardView in Android With Example
Android RecyclerView in Kotlin
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Stream In Java
Object Oriented Programming (OOPs) Concept in Java