【问题标题】:Getting null value from intent in deep link从深层链接中的意图获取空值
【发布时间】:2019-04-10 18:30:39
【问题描述】:

我在我的应用程序中添加了一个深层链接,它将活动映射到我网站上的特定网页(链接参考:https://developer.android.com/training/app-links/deep-linking)。 在我的 Java 代码中处理深层链接时,getAction()getData() 方法给了我 null 值。 我在这里尝试对其进行测试:https://firebase.google.com/docs/app-indexing/android/test(这给了我完美的结果)但是单击时,相同的链接会在网络浏览器中而不是在我的应用程序中打开。

Android 清单代码:

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"
    android:exported="true">

    <tools:validation testUrl="https://www.mywebsite.com/xyz" />
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="https"
            android:host="www.mywebsite.com"
            android:pathPrefix="/xyz" />
    </intent-filter>
</activity>

Java 代码:

    Intent intent = getIntent();
    String action = intent.getAction(); // getting null value here
    Uri data = intent.getData(); // getting null value here

我希望如果应用程序存在,那么在单击链接时应该打开它,否则链接将打开网页。 我在哪里错过了什么?

【问题讨论】:

  • 在我的 Android 应用中实现深层链接时,我也面临同样的问题。
  • 我在我的安卓应用程序中使用谷歌提供的深层链接。我已将我的 android 应用程序的活动映射到我网站的网页。我想知道,如果我们单击该特定页面的链接,它应该将其重定向到我的应用程序并在应用程序存在时打开该特定活动或者在浏览器中打开网页。如果可能,请协助我解决问题。谢谢!
  • @AnirudhMishra,你找到解决方案了吗?
  • 您是否尝试过关闭/杀死您的应用程序然后运行测试?因为可能的问题是您从应用程序开始就已经有了意图。在这种情况下,您必须使用 override onNewIntent 函数检查新意图。
  • @TomasIvan,谢谢 - 你完全正确。为您的答案添加了加号:)

标签: java android firebase indexing deep-linking


【解决方案1】:

您可能会遇到这个问题,因为使用了singleTask。在这种情况下,您应该使用onNewIntent 来“更新”意图,因为onCreateonResume 不会处理它。

如果活动在其包中将 launchMode 设置为“singleTop”,或者客户端在调用 startActivity(Intent) 时使用了 Intent#FLAG_ACTIVITY_SINGLE_TOP 标志,则会调用此方法。在任何一种情况下,当活动在活动堆栈的顶部而不是正在启动的活动的新实例时重新启动时,onNewIntent() 将在具有用于重新启动的 Intent 的现有实例上调用它。

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    //TODO: do something with new intent
}

【讨论】:

    【解决方案2】:

    不是在 onCreate 中获取意图数据,而是在 onResume 中获取

       @Override
    protected void onResume() {
        super.onResume();
    
        Intent intent = getIntent();
        if (intent != null && intent.getData() != null ){
            Toast.makeText(this, intent.getData().toString(), Toast.LENGTH_SHORT).show();
            webView.loadUrl(intent.getData().toString());
        }
    }
    

    添加这些附加活动的代码行以进行深度链接以打开您的网站页面

    如果它帮助您解决问题,请将其标记为答案

    【讨论】:

    • 该链接已在网络浏览器中打开网页,我希望该链接可以打开我的应用的 MainActivity。感谢您的帮助!
    • 它不起作用 - 数据仍然为空的意图。我试图在 Start 和 onResume 上使用它,但仍然没有运气
    猜你喜欢
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多