Android Deep Link URL to App Activity Tutorial

Android provide deep links open a specific activity within an app with data to it. Deep link is very useful because user able to navigate website URL to the specific activity in the app instantly to get better user experiences. Besides, it may also particularly useful for actions, such as clicking a notification or sending an app link via email or messaging. When the user clicks the email notification she/he received, it opens a deep link that takes her/his to the email in the app. Deep links also allow Google to index your app and link to specific sections of your app in searches engine. In this tutorial, I will teach you how to use the android deep link in your project.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “DeepLinkExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many Android devices currently support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit AndroidManifest.xml

Add intent filter action VIEW and category DEFAULT + BROWSABLE in the MainActivity to activate Deep Link feature. Besides, add host and schema inside the intent filter section.

  <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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="https"
                    android:host="www.example.com" />
            </intent-filter>
            <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" />
            </intent-filter>

        </activity>

Edit MainActivity.java class

Go to your MainActivity class and create a method name “handle” to get intent from the deep link. onNewIntent is for activity “singleTop”, so it will not open duplicated activity and call the onCreate method again.

public class MainActivity extends AppCompatActivity {

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

        handleIntent(getIntent());

        // ATTENTION: This was auto-generated to handle app links.
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        handleIntent(intent);

    }

    private void handleIntent(Intent intent) {

        String appLinkAction = intent.getAction();
        String appLinkData = intent.getDataString();
        try {
            if (Intent.ACTION_VIEW.equals(appLinkAction) && appLinkData != null) {

                String afterDecode = URLDecoder.decode(appLinkData, "UTF-8");

                Toast.makeText(getApplicationContext(),afterDecode,Toast.LENGTH_LONG).show();


            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

Run Your Project

Lastly, you can now run your project in your device or emulator.

Test Your Deep Link

Open your terminal and insert the following command.

  1. set PATH=%PATH%;C:\Users\(USERNAME)\AppData\Local\Android\sdk\platform-tools (if adb not found)
  2. adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d “//www.example.com/gizmos”

Android Deep Link URL to App Activity

Source Code

(Visited 718 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

1 Response

  1. Oakywood says:

    Oakywood shop is an online store with products from oak, like iphone charging dock.
    This is our flagship products, which is necessary to buy himself.
    Welcome to online shopping and ordering things to copyright.

    The combination of electronics and wood is unique. Shop
    around the clock, seven days a week.

Leave a Reply

Your email address will not be published. Required fields are marked *