从深层链接的意图中获取空值

Ani*_*hra 5 java indexing android deep-linking firebase

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

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

Java代码:

    Intent intent = getIntent();
    String action = intent.getAction(); // getting null value here
    Uri data = intent.getData(); // getting null value here
Run Code Online (Sandbox Code Playgroud)

我希望如果该应用程序存在,则应在单击链接时将其打开,否则该链接将打开网页。我在哪里和想念什么?

Tom*_*van 8

您可能会遇到此问题,因为使用singleTask. 在这种情况下,您应该使用onNewIntent来“更新”意图,因为onCreate并且onResume不会处理它。

对于在其包中将 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
}
Run Code Online (Sandbox Code Playgroud)


小智 3

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

添加以下代码行,附加活动以进行深层链接以打开您的网站页面

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