从深层链接的意图中获取空值 [英] Getting null value from intent in deep link
问题描述
我向我的应用程序添加了一个深层链接,该链接将活动映射到我网站上的特定网页(链接称为: https://developer.android.com/training/app-links/deep-linking )。
在处理Java代码中的深层链接时, getAction()
和 getData()
方法给我空值。
我尝试在此处进行测试: https://firebase.google.com / docs / app-indexing / android / test (这给了我完美的结果),但单击该链接时,它会在Web浏览器中而不是在我的应用程序中打开。
I have added a deep link to my app which maps an activity to a particular web page on my website (Link referred: https://developer.android.com/training/app-links/deep-linking ).
While handling the deep link in my Java code, getAction()
and getData()
methods give me null value.
I tried testing it here: https://firebase.google.com/docs/app-indexing/android/test (This gave me perfect result) But the same link opens in A web browser rather than in my app when clicked.
Android清单代码:
Android Manifest code :
<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
我希望如果该应用存在,则应在打开应用时打开单击该链接,否则该链接将打开网页。
我在哪里和想念什么?
I want that if the app is present, then it should be opened when the link is clicked or else the link will open the web page. Where and what am I missing?
推荐答案
您可能会遇到此问题,因为使用 singleTask
。在这种情况下,您应该使用 onNewIntent 更新意图,因为 onCreate
和 onResume
不会处理它。
You can run into this problem because using singleTask
. In that case you should use onNewIntent to 'update' intent, because onCreate
and onResume
will not handle it.
在其包中将launchMode设置为 singleTop的活动,或者如果客户端在调用startActivity(Intent)时使用了Intent#FLAG_ACTIVITY_SINGLE_TOP标志,则调用此方法。在这两种情况下,当在活动堆栈的顶部重新启动活动而不是启动活动的新实例时,将使用已用于重新启动的Intent在现有实例上调用onNewIntent()
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
//TODO: do something with new intent
}
这篇关于从深层链接的意图中获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!