首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过深度链接传递数据?

问如何通过深度链接传递数据?
EN

Stack Overflow用户
提问于 2015-08-08 12:46:03
回答 3查看 14.3K关注 0票数 7

我的应用程序中有一个优惠列表,每个列表项上都有一个共享按钮。当任何用户单击共享链接时,我都会使用深度链接打开应用程序的优惠详细信息活动。我的详细信息页面活动在有人点击链接时被触发,但我如何知道,当某人点击共享的深度链接时,将显示哪些优惠详细信息活动。

EN

回答 3

Stack Overflow用户

发布于 2017-02-21 18:48:47

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

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

然后,在该活动中,您可以执行以下操作

代码语言:javascript
复制
Uri data = intent.getData();

data.getQueryParameter("key");
票数 17
EN

Stack Overflow用户

发布于 2020-09-23 17:41:49

下面的@Vikas answer是完成此操作的简化方法-

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

代码语言:javascript
复制
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);

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

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

用于处理深度链接的应用程序2的清单文件将intent filter添加到将处理数据的相应活动,在我的示例中,MainActivity将执行此操作

代码语言:javascript
复制
<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>

然后在你的活动中,你可以得到这样的数据:

代码语言:javascript
复制
 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");

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

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

票数 3
EN

Stack Overflow用户

发布于 2015-10-09 19:02:36

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

代码语言:javascript
复制
<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>

举这个例子,如果应用程序深度链接到这里,你可以在相应的活动(这里: com.example.android.GizmosActivity)中接收你的意图,并从那里本身提取信息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31889744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档