Native Mobile App Development With Java

DeepLink Android Example

30-Sep-2024

DeepLink Android Example

Output:


for testing, we put the link in the message app which we want to open in our app











by using the deep links in Android, you can open your app by clicking a link 



1/  Create A New Project: ( DeepLink Android Example )


2/  Modify your AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DeepLinkAndroidExample"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

<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="tutorialb.com"
android:pathPrefix="/post" />
             <data
android:scheme="http"
android:host="tutorialb.com"
android:pathPrefix="/post" />
</intent-filter>
</activity>
</application>

</manifest>



1/  In the intent filter data tag here you can put your website hostname and the path prefix to open the link in your app



<data
android:scheme="https"
android:host="tutorialb.com"
android:pathPrefix="/post" />



1/  Modify your MainActvity.java


to know which link you opened in your app



package com.example.deeplinkandroidexample;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

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

if (data != null) {
String deepLinkInfo = data.toString();
Toast.makeText(this, "Deep Link Data: " + deepLinkInfo, Toast.LENGTH_SHORT).show();
}
}
}
















Comments