我在我的应用程序中添加了一个深层链接,该链接将活动映射到我网站上的特定网页(链接称为: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>
Java代码:
Intent intent = getIntent();
String action = intent.getAction(); // getting null value here
Uri data = intent.getData(); // getting null value here
我希望如果存在该应用程序,则应在单击链接时将其打开,否则该链接将打开网页。 我在哪里和想念什么?
答案 0 :(得分:2)
因为使用singleTask
,所以您可能会遇到此问题。在这种情况下,您应该使用onNewIntent来“更新”意图,因为onCreate
和onResume
不会处理它。
对于在其程序包中将launchMode设置为“ singleTop”的活动,或者如果客户端在调用startActivity(Intent)时使用了Intent#FLAG_ACTIVITY_SINGLE_TOP标志,则调用此方法。在这两种情况下,当在活动堆栈的顶部重新启动活动而不是启动活动的新实例时,将使用已用于重新启动的Intent在现有实例上调用onNewIntent()它。
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
//TODO: do something with new intent
}
答案 1 :(得分:0)
不要在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());
}
}
添加以下几行代码,并附上用于深度链接的活动以打开您的网站页面
并将其标记为答案,如果它可以帮助您解决问题