How to pass data through deep linking?
Asked Answered
G

3

13

I am having a list of offers in my application and there is a share button on every list item.I am using deep link to open a offer detail activity of my application when any user clicks on the shared link.i am in a situation that my detail page activity is being triggered when someone clicks the link ,But how can i know ,which offer detail activity to show when some one clicks on the shared deep link .

Godolphin answered 8/8, 2015 at 4:46 Comment(2)
You can read the data of the intent that launched the activity, get the offer information based on the specific URL, then use that to display the right activity. More details about reading launch intents here: developer.android.com/training/app-indexing/…Assess
Can we remove Java tag from this question? I am looking for deeplinking with java and not android. I think we can agree that both domain are separate and this question would not help people with Java + deeplink questionTenaculum
H
20

The manifest file will remain same as specified in this link https://developer.android.com/training/app-indexing/deep-linking.html

But you can provide the extra data in the link you send to the user as www.example.com/gizmos?key=valueToSend

then in the activity you can do something like

Uri data = intent.getData();

data.getQueryParameter("key");
Heartwhole answered 21/2, 2017 at 10:48 Comment(1)
@MustafaShaikh I didn't try this but maybe you could use "&key2=value2" for another key value pair.Heartwhole
A
6

Following @Vikas answer here is a simplified way to do this -

Application 1: This application will send data to another app through deeplinking

String deepUrl = "app://anotherapp?key1=value1&key2=value2"; //key1 and key2 for sending data to other application
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.setData (Uri.parse(deepUrl));
startActivity(intent);

this will start a intent for lunching another app with some data as path parameter with the link.

Now we need to handle the corresponding link to handle in our second application like this:

Application 2's Manifest file to handle deeplinking add intent filter to the corresponding activity which will handle the data, in my case MainActivity will do

<activity android:name=".MainActivity">
            ...
            <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="anotherapp"
                    android:scheme="app" />
            </intent-filter>
        </activity>

and then in your activity you can get data like this:

 Intent intent = getIntent();
 Uri data = intent.getData();
 String data1= data.getQueryParameter("key1"); // you will get the value "value1" from application 1 
 String data2= data.getQueryParameter("key2");

For more information about deep linking see the official link

Notes: If Application 2 not installed in your phone then you can get an error from application 1 stating ActivityNotFoundException

Altamira answered 23/9, 2020 at 9:41 Comment(0)
T
2

Supposing that you generate a separate share link for each item. You could send some parameters along with the deep link URL and then receive them in the App. Any kind of an ID would suffice. (Source: this)

<intent-filter android:label="@string/filter_title_viewgizmos">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
    <data android:scheme="http"
          android:host="www.example.com"
          android:pathPrefix="/gizmos" />
    <!-- note that the leading "/" is required for pathPrefix-->
    <!-- Accepts URIs that begin with "example://gizmos”
    <data android:scheme="example"
          android:host="gizmos" />
    -->
</intent-filter>

Taking up this example, if the app deep links here, you can receive your intent in the corresponding activity (Here: com.example.android.GizmosActivity) and extract information from there itself.

Tonneau answered 9/10, 2015 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.