简体   繁体   English

在深层链接中从intent获取null值

[英]Getting null value from intent in deep link

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 ). 我添加了一个指向我的应用程序的深层链接,该链接将活动映射到我网站上的特定网页(链接参考: 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. 在处理我的Java代码中的深层链接时, getAction()getData()方法给出了null值。 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. 我尝试在这里测试它: https//firebase.google.com/docs/app-indexing/android/test (这给了我完美的结果)但是同样的链接在A浏览器中打开而不是在我的应用程序中单击时打开。

Android Manifest code : 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 Code : 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? 我错过了哪里和什么?

You can run into this problem because using singleTask . 您可以遇到此问题,因为使用singleTask In that case you should use onNewIntent to 'update' intent, because onCreate and onResume will not handle it. 在这种情况下,您应该使用onNewIntent来“更新”意图,因为onCreateonResume将不会处理它。

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). 这被称为在其包中将launchMode设置为“singleTop”的活动,或者在调用startActivity(Intent)时客户端使用Intent #FLAG_ACTIVITY_SINGLE_TOP标志。 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. 在任何一种情况下,当在活动堆栈的顶部而不是正在启动的活动的新实例重新启动活动时,将在现有实例上使用用于重新启动的Intent调用onNewIntent()它。

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

    //TODO: do something with new intent
}

instead of getting intent data in onCreate, get in onResume 而不是在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());
    }
}

add these lines of code which activity is attached for deep linking to open your website page 添加这些代码行,附加活动以进行深层链接以打开您的网站页面

and mark it as a answer if it helped you to solve your problem 如果它能帮助您解决问题,请将其标记为答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM