【问题标题】:How to give Intent Data in Deep Link如何在深度链接中提供意图数据
【发布时间】:2017-02-15 10:11:50
【问题描述】:

我已更新清单文件以支持深度链接。我已经从 Run(Edit Configuration) 中检查了它,它会打开指定的活动。

现在我需要使用深层链接发送一些数据。那么它的程序应该是什么。我添加了另一个数据属性,但我不明白如何以相同的键/值方式在 Activity 中获取数据。

我在 Activity 中得到这样的 Intent

   Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();

intent.getData() 有这个值= myapp://videodeeplink

我已经阅读了一些关于此的文章和教程,但我无法得到这个。请指导我如何在深度链接中放置和获取一些数据。

myapp://videodeeplink

<activity
    android:name=".VideosListActivity"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme" >

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="myapp" android:host="videodeeplink"/>
        <data android:scheme="videoURL" android:host="videoURL"/>
    </intent-filter>


</activity>

【问题讨论】:

    标签: android performance android-layout android-fragments deep-linking


    【解决方案1】:

    要发送数据,您应该在数据标签中添加 pathPrefix 参数,如下所示,以便稍后我们可以在您调用它的 Activity/Fragment 中解析它。

     <data
         android:host="@string/host"
         android:pathPrefix="path"
         android:scheme="@string/schema" />
    

    现在当你想解析它时,你可以在 Android 中使用 pathSegments,如下所示,使用 Intent 来获取里面的数据。

    mainIntent = getIntent();
    if (mainIntent!=null && mainIntent.getData()!=null
        && (mainIntent.getData().getScheme().equals("http"))){
            Uri data = mainIntent.getData();
            List<String> pathSegments = data.getPathSegments();
            if(pathSegments.size()>0)
            String prefix=pathSegments.get(0); // This will give you prefix as path
    }
    

    【讨论】:

    • 谢谢,那么最终用户将如何创建深层链接?我们是否需要在深层链接 URL 中添加前缀?myapp://videodeeplink/pathValue ?
    • 您可以做的是将前缀更改为 path/data1/data2/ 。因此,当您遍历 pathSegements 列表时,请使用 pathSegments.get(1) 等来获取您的数据并使用 myapp://videodeeplink/pathValue 作为深度链接 url
    • 谢谢,让我试试这个:)
    • 与此相关的最后一个问题,如何将长 URL 传递给 Path,例如 youtube.com/watch?v=aaaa
    • 如果您想这样做,那么最好使用 PendingIntent 并在您的意图中保留您想要的任何内容,并在点击项目时关联待处理的意图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    相关资源
    最近更新 更多