如何在Android中从深链接中获取值?

3
我正在实现Android中的深度链接,我想要做的是当用户点击链接时,将其重定向到应用程序,并且这已经可以工作,但我还希望用户能够看到我在链接中传递的一些值。
例如:我的链接是:https://examplelink.com/123456789。应用程序需要获取123456789代码。
我该怎么做呢?
注意:我已经在清单文件中添加了以下内容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsharedlink">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "example://gizmos” -->
            <data android:scheme="https"
                android:host="examplelink.com" />
        </intent-filter>
    </activity>
</application>

我通过以下方式解决了问题:

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

String[] link = data.toString().split("/");

txt.setText(link[3]);

感谢您的帮助。
1个回答

3
回复内容存在敏感词^**$到清单中。
<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="your host name"
                    android:pathPrefix="your path"
                    android:scheme="https" />


            </intent-filter>

这是我们的应用链接: https://myapp.page.link/path?email=your@gmail.com&token=12345 在我的闪屏活动中,我使用 Kotlin 代码获取数据:
Firebase.dynamicLinks
            .getDynamicLink(intent)
            .addOnSuccessListener(this) { pendingDynamicLinkData ->
                // Get deep link from result (may be null if no link is found)
                val data: Uri? = intent?.data
                // get path
                val path = data?.path
                // get param
                val email = data?.getQueryParameter("email").toString()

            }.addOnFailureListener(this) { e -> MLog.e(TAG, "getDynamicLink:onFailure $e") }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接