【问题标题】:How to get full deeplink url from intent?如何从意图中获取完整的深度链接网址?
【发布时间】:2022-01-05 15:27:47
【问题描述】:

我的应用处理深层链接。当我尝试检索我的深层链接 URL 时,我得到的不是完整的 URL(没有查询参数)。

我的 AndroidManifest:

  <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />

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

                <data
                    android:host="www.test.com"
                    android:scheme="testlink" />
            </intent-filter>

例如,我需要处理一个深层链接: testlink://www.test.com/strategy?year=2021

我正在尝试获取数据的内部活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
    String url = getIntent().getData().toString();
 
}

我得到一个这样的 URL:testlink://www.test.com/strategy?但预期 testlink://www.test.com/strategy?year=2021 。查询参数被剪裁。

为什么会发生这种情况?请帮帮我。

注意: 使用 HTTPS 或 HTTP 方案时,我按预期从意图中获取 URL,但是当我使用自定义方案时,查询参数丢失

【问题讨论】:

  • 发布主代码。我无法从您的代码中找到 Intent。
  • 您是否使用 adb 来测试深层链接?有一个已知的错误。

标签: android android-intent uri intentfilter android-deep-link


【解决方案1】:

你必须用这个字符'?' 拆分你的Uri 才能得到year=2021 这样。

Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();

if (Intent.ACTION_VIEW.equals(appLinkAction) && appLinkData != null) {
    String lastUrlId = appLinkData.getLastPathSegment(); // strategy?year=2021
    String[] segments = lastUrlId.split("?");
    ....
}

【讨论】:

    【解决方案2】:

    在应用清单中的 Activity 意图过滤器中包含以下内容:

                <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="www.test.com"
                        android:scheme="testlink" />
                </intent-filter>
    

    然后在该活动的 onCreate 函数中,您可以使用:

    Uri uri = this.getIntent().getData();
    

    从意图中检索android.net.Uri

    如果您想从 Uri 获取更多信息,您可以使用:

    //To get the full url passed in use
    uri.toString();
    
    //To get the data after the '?' only use
    uri.getEncodedQuery();
    

    从这里:https://stackoverflow.com/a/58346741/2396026

    以上代码已在 API30 x86 仿真器映像上测试并运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2018-04-19
      • 2017-04-09
      • 1970-01-01
      相关资源
      最近更新 更多