如何访问 Android Instant App 的模块并将参数传递给模块

How to access and pass parameters to the modules of an Android Instant App

对于正常安装的应用程序,可以使用 深度链接 技术,以便不仅可以从 URL 打开特定应用程序,还可以将其重定向到特定 section/function 例如特定 Facebook post 或地图上的特定坐标。

自从我读到使用 Instant Apps 这将是不可能的,因为 links 已经指向要下载的特定模块并且 运行,怎么可能访问不只有所述模块还传递了一些参数?

例如:

这是 link 我的地图应用程序的仅查看模块将从中下载:“www.myinstantappexample.com/onlyviewmap

如果我想让它指向一组特定的坐标,link 将如何组成? 会不会是这样:"www.myinstantappexample.com/onlyviewmap/?x=0.000&y=0.000" ?

据我所知,google 没有涵盖这方面的内容,我真的无法理解它。

即时应用程序和深度 Linking

即时应用程序 rely on App Links 可以工作,而应用程序 Link 只是深度 link 的一种类型。如此深入 linking 对于 Instant Apps 来说仍然是可能的,事实上 绝对关键 它们的功能。但是,linking 深度 URI 方案(在 Android 应用程序中仍然非常普遍) 受支持。

常规应用程序和即时应用程序之间的区别在于,设备只会加载单个 Activity 以响应用户点击的应用程序 Link,而不需要下载完整的通过 Play 商店打包。这对用户来说是一种更加无缝的体验,但底层技术的工作方式相同。

传递自定义参数

如果用户单击 Links-enabled URL 应用程序,例如 http://www.myinstantappexample.com/onlyviewmap/?x=0.000&y=0.000,您将在应用程序打开后将整个字符串返回到应用程序中。您必须自己解析 xy 变量,但您可以使用它们。像这样:

Uri data = this.getIntent().getData();
if (data != null && data.isHierarchical()) {
    String uri = this.getIntent().getDataString();
    Log.i("MyApp", "Deep link clicked " + uri);
}

您只需操纵 uri 值即可找到您需要的内容。

自定义参数的替代方法

或者,您可以使用 Branch.io (full disclosure: I'm on the Branch team) to power your links. We have full support for Instant Apps,这样您就可以使用更加友好的数据格式。我们让您像这样创建 links,以控制行为的每个部分:

branch.link({
    tags: [ 'tag1', 'tag2' ],
    channel: 'facebook',
    feature: 'dashboard',
    stage: 'new user',
    data: {
        x: '0.000',
        y: '0.000',
        '$desktop_url': 'http://myappwebsite.com',
        '$ios_url': 'http://myappwebsite.com/ios',
        '$ipad_url': 'http://myappwebsite.com/ipad',
        '$android_url': 'http://myappwebsite.com/android',
        '$og_app_id': '12345',
        '$og_title': 'My App',
        '$og_description': 'My app\'s description.',
        '$og_image_url': 'http://myappwebsite.com/image.png'
    }
}, function(err, link) {
    console.log(err, link);
});

在 return 中你会得到一个 URL 就像 http://myappname.app.link/iDdkwZR5hx,然后在点击 link 之后在应用程序中,你会得到如下所示的东西:

{
    tags: [ 'tag1', 'tag2' ],
    channel: 'facebook',
    feature: 'dashboard',
    stage: 'new user',
    data: {
        x: '0.000',
        y: '0.000'
    }
}

If I want it to point to a specific set of coordinates how would the link be composed?

如何在 URL 中包含任何附加信息由您决定。它可以通过 URL 参数或在路径本身中。例如

https://www.myinstantappexample.com/location/2/user/5 https://www.myinstantappexample.com/onlyviewmap/?x=1.2&y=3.4

然后您在接收 Activity 中解析 URL。 Uri class includes a number of helper methods such as getQueryParameter() and getPathSegments() 使这更容易。

例如,解析这个URL:

https://www.myinstantappexample.com/onlyviewmap/?x=1.2&y=3.4

你会在你的Activity中做这样的事情:

Uri uri = getIntent().getData();
String x;
String y;
if (uri != null) {
  x = uri.getQueryParameter("x"); // x = "1.2"
  y = uri.getQueryParameter("y"); // y = "3.4"
}
if (x != null && y != null) {
  // do something interesting with x and y
}

为此,您必须使用 "app links assistant" 工具->App Links Assistant

然后检查您的 link,并在路径选择器中检查是否选择了 "pathPrefix" 选项。

然后在您的 activity 的 OnCreate 方法底部(与您最近编辑的 link 有关)添加此代码:

Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();
// then use appLinkData.getQueryParameter("YourParameter")

您可以测试和调试它,使用 "editConfigurations" 选项,只需打开 window 并编辑您的 instantApp 模块(使用您最近编辑的 Link 启动的模块)和在 URL 字段中添加您需要的 URL 参数。 (然后 运行 那个模块 :D )

希望对您有所帮助。