深度链接没有打开指定的活动,而是打开了其他活动。

huangapple go评论86阅读模式
英文:

Deep link not opening specified Activity but opens other Activity

问题

我已经通过编程方式创建了一个DynamicLink,其中包括子域名语法、一些标题和预览图像。我还指定了当链接被点击时应该打开的IntentFilter。但是当点击链接时,它会打开另一个具有深层链接的活动。页面链接域名由谷歌在Firebase控制台中提供。创建动态链接的代码如下:

String e = "https://learnandroid.page.link/?link=https://learn.android/&apn=com.learnandro&amv=16&st=Please view the ContentEvent&si=" + some Image Url + "&afl=https://play.google.com/store/apps/details?id=app Package name";

Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance()
    .createDynamicLink()
    .setLongLink(Uri.parse(e))
    .buildShortDynamicLink()
    .addOnCompleteListener(new OnCompleteListener<ShortDynamicLink>() {
        @Override
        public void onComplete(@NonNull Task<ShortDynamicLink> task) {
            Uri get = task.getResult().getShortLink();
            Intent sh = new Intent(Intent.ACTION_SEND);
            sh.setType("text/plain");
            sh.putExtra(Intent.EXTRA_TEXT, "View the Amazing Event " + get);
            startActivity(Intent.createChooser(sh, "View"));
        }
    });

活动的意图过滤器如下:

<activity android:name=".content.Home"
    android:launchMode="singleTask"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="learn.android"
            android:scheme="https" />
    </intent-filter>
</activity>

但是,当点击生成的链接时,它会打开另一个活动。

英文:

I have created a DynamicLink programatically which includes sub-domain syntax, some title and preview Image. I have also specified the IntentFilter which should open that activity when clicked. But when the link is clicked it open another Activity which also have Deep link. page.link domain is provided by google itself in Firebase Console
The code for creating Dynamic Link is

 String e=&quot;https://learnandroid.page.link/?link=https://learn.android/&amp;apn=com.learnandro&amp;amv=16&amp;st=Please view the ContentEvent&amp;si=&quot;+some Image Url+&quot;&amp;afl=https://play.google.com/store/apps/details?id=app Package name&quot;;

Task&lt;ShortDynamicLink&gt; shortLinkTask = FirebaseDynamicLinks.getInstance()
                        .createDynamicLink()
                        .setLongLink(Uri.parse(e))
                        .buildShortDynamicLink()
                        .addOnCompleteListener(new OnCompleteListener&lt;ShortDynamicLink&gt;() {
                            @Override
                            public void onComplete(@NonNull Task&lt;ShortDynamicLink&gt; task) {
                                Uri get=task.getResult().getShortLink();
                                Intent sh=new Intent(Intent.ACTION_SEND);
                                sh.setType(&quot;text/plain&quot;);
                                sh.putExtra(Intent.EXTRA_TEXT,&quot;Vi                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ew the Amazing Event   &quot;+get);
                                startActivity(Intent.createChooser(sh,&quot;View&quot;));
                            }
                        });  

The Intent filter for Activity is given as

&lt;activity android:name=&quot;.content.Home&quot;
            android:launchMode=&quot;singleTask&quot;
            android:screenOrientation=&quot;portrait&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.VIEW&quot;/&gt;
                &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
                &lt;data
                    android:host=&quot;learn.android&quot;
                    android:scheme=&quot;https&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;  

But when the link generated is clicked it opens some Another activity.

答案1

得分: 1

如果您在多个活动中使用深链接,则应使用 android:pathPattern 将它们区分开来。

以下是示例代码:

<data
    android:host="learn.android"
    android:pathPattern="/home"
    android:scheme="https" />

然后将 /home 添加到您的链接中:

String e = "https://learnandroid.page.link/?link=https://learn.android/home/&apn=com.learnandro&amv=16&st=Please view the ContentEvent&si=" + 某个图片网址 + "&afl=https://play.google.com/store/apps/details?id=应用程序包名";
英文:

If you have deeplink in multiple activities then you should use android:pathPattern to differentiate them from each other.

Here is sample code

&lt;data
    android:host=&quot;learn.android&quot;
    android:pathPattern=&quot;/home&quot;
    android:scheme=&quot;https&quot; /&gt;

and add /home to your link

String e=&quot;https://learnandroid.page.link/?link=https://learn.android/home/&amp;apn=com.learnandro&amp;amv=16&amp;st=Please view the ContentEvent&amp;si=&quot;+some Image Url+&quot;&amp;afl=https://play.google.com/store/apps/details?id=app Package name&quot;;

答案2

得分: 0

以下是已翻译的内容:

您可以尝试以下代码片段

val data: Uri? = intent?.data
if (Uri.EMPTY != data) 
{
    val id = intent.data?.getQueryParameter("ID")    
    intent = Intent(this, ActivityName::class.java)    
    startActivity(intent)  
    finish()
}

- 在 getQueryParameter 中您需要传递包含 URL 中数据的键名
- 在清单文件中定义此代码集合的活动
英文:

You can try below snippet :

     val data: Uri? = intent?.data
     if (Uri.EMPTY != data) 
     {
        val id = intent.data?.getQueryParameter(&quot;ID&quot;)    
        intent = Intent(this, ActivityName::class.java)    
        startActivity(intent)  
        finish()
     }
  • In getQueryParameter, you need to pass KEY name which contains data in URL
  • Define this set of code in Activity which is defined in Manifest file

huangapple
  • 本文由 发表于 2020年1月6日 20:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定