如何通过深层链接传递数据?

5 java android facebook deep-linking

我在我的应用程序中有一个要约列表,每个列表项都有一个共享按钮.当任何用户点击共享链接时,我正在使用深层链接打开我的应用程序的要约详细活动.我在这种情况下当有人点击链接时,我的详细信息页面活动会被触发,但我怎么知道,当有人点击共享的深层链接时,会显示详细活动.

小智 15

清单文件将保持与此链接中指定的相同https://developer.android.com/training/app-indexing/deep-linking.html

但您可以在发送给用户的链接中提供额外数据,如www.example.com/gizmos?key=valueToSend

然后在活动中你可以做类似的事情

Uri data = intent.getData();

data.getQueryParameter("key");
Run Code Online (Sandbox Code Playgroud)


Mos*_*lah 6

以下@Vikas 的回答是一种简化的方法 -

应用程序1:该应用程序将通过深度链接将数据发送到另一个应用程序

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);
Run Code Online (Sandbox Code Playgroud)

这将启动另一个应用程序的意图,其中一些数据作为链接的路径参数。

现在我们需要在第二个应用程序中处理相应的链接,如下所示:

应用程序 2 的 清单文件用于处理深度链接,添加intent filter到将处理数据的相应活动中,在我的情况下MainActivity会这样做

<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>
Run Code Online (Sandbox Code Playgroud)

然后在您的活动中您可以获得如下数据:

 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");
Run Code Online (Sandbox Code Playgroud)

有关深度链接的更多信息,请参阅官方链接

注意:如果您的手机中未安装应用程序 2,那么您可能会从应用程序 1收到错误消息,指出ActivityNotFoundException


Shi*_*xit 2

假设您为每个项目生成单独的共享链接。您可以发送一些参数以及深层链接 URL,然后在应用程序中接收它们。任何类型的 ID 就足够了。\n(来源:this

\n\n
<intent-filter android:label="@string/filter_title_viewgizmos">\n    <action android:name="android.intent.action.VIEW" />\n    <category android:name="android.intent.category.DEFAULT" />\n    <category android:name="android.intent.category.BROWSABLE" />\n    <!-- Accepts URIs that begin with "http://www.example.com/gizmos\xe2\x80\x9d -->\n    <data android:scheme="http"\n          android:host="www.example.com"\n          android:pathPrefix="/gizmos" />\n    <!-- note that the leading "/" is required for pathPrefix-->\n    <!-- Accepts URIs that begin with "example://gizmos\xe2\x80\x9d\n    <data android:scheme="example"\n          android:host="gizmos" />\n    -->\n</intent-filter>\n
Run Code Online (Sandbox Code Playgroud)\n\n

以这个例子为例,如果应用程序在此处进行深层链接,您可以在相应的活动(此处:com.example.android.GizmosActivity)中接收您的意图,并从那里提取信息。

\n