【问题标题】:how to make links from my website to direct to my app if installed如果已安装,如何从我的网站链接到我的应用程序
【发布时间】:2020-12-24 02:30:29
【问题描述】:

我想要实现的目标

当用户点击我网站上的任何链接时,我希望它在我的应用中引导用户特定的链接(如果已安装)。如果未安装,则引导用户在浏览器中进行链接。

示例:

用户收到一封电子邮件,其中包含一个名为:www.myapp.com/someplaceinmyapp 的链接,如果已安装,它将转到我的应用程序中的该页面。如果没有,则在浏览器中转到该页面。

请记住,它将适用于我应用中的所有页面。所以这适用于我网页上的任何链接。

我的问题

这个有具体的名字吗?我知道深度链接和应用程序链接,但我不确定哪一个适合我。

这甚至可能吗?我什至不确定目前是否有可能。

此代码会在网页或应用程序中吗?例如:Javascript 或 Kotlin 或清单文件中的内容。

我该怎么做?这可能是我最需要的东西。

注意事项

我的网页中有一些内容显示“使用该应用程序”的弹出窗口(仅在浏览器中)。如果没有安装,那就去play store。如果已安装,则在我的应用程序中转到我网站的主页。我从here 找到了如何使用投票最多的答案(不是被接受的答案),所以在我的清单中我有:

        <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:scheme="http"
                android:host="www.myapp.com"
                android:pathPrefix="/install.html" />
        </intent-filter>

如果您有任何想添加到我的问题中的内容,我很乐意添加。

【问题讨论】:

  • 你的意思是这样? youtu.be/-Vpf2phPoF0
  • 是的,这正是我所需要的。
  • 就像视频描述中所说的那样,是的,我想要一个关于它的教程。

标签: android android-manifest


【解决方案1】:

我发现这样做的方法是使用 App Links 助手,您可以在 android studio 中的 Tools -> App Links assistant 下找到它。这是您打开后应该在侧面看到的内容:

第 1 步

单击“打开 URL 映射编辑器”,然后添加 URL 映射(单击“+”)。 在主机中,输入您的网站链接,例如:https://www.example.com。对于路径,在选择框中,只选择“路径”并且在类型框中不输入任何内容。

第 2 步

选择活动按钮不适用于 Kotlin,因此这里是您需要手动输入的代码。

    override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    handleIntent(intent)
}

private fun handleIntent(intent: Intent) {
    val wbWebView = findViewById<View>(R.id.your_webview) as WebView
    val appLinkAction = intent.action
    val appLinkData = intent.data
    if (Intent.ACTION_VIEW == appLinkAction && appLinkData != null) {
            wbWebView.loadUrl(appLinkData.toString())
    } else {
        // default link that it goes to when opening app
        wbWebView.loadUrl("https://www.example.com")
    }
}

在你的 onCreate 添加handleIntent(intent)

第 3 步

单击“打开数字资产链接文件生成器”。大部分信息已经填写完毕,因此您可以继续单击“生成数字资产链接文件”。在您的网站中创建一个名为“.well-known”的文件夹,然后在其中放置一个名为“assetlinks.json”的文件,将预览粘贴到那里。因此,当您转到 https://www.yourwebsite.com/.well-known/assetlinks.json 时,您应该能够看到数字资产链接文件。您还可以查看其他网站的数字资产链接文件。就像谷歌一样,https://www.google.com/.well-known/assetlinks.json

然后继续点击“链接并验证”按钮,如果一切顺利,您应该会在按钮下方看到:

第四步

是时候测试了!继续并单击“测试应用程序链接”并输入您网站中的 URL。如果一切顺利,您应该不会看到消歧对话框。

我还想通过向自己发送一封包含链接的电子邮件来做进一步的测试。

备注

如果您的网站使用 https 而不是 http,则只能使用 App Links Assistant。

我在清单中的活动标签中添加了android:launchMode="singleTask",这意味着如果您单击电子邮件中网站中的链接,它将在新窗口中打开。

他们有一个关于这个here 的文档,其中包括一个视频(请记住,他们使用的是 java)

【讨论】:

  • 如果有人对此需要更多说明或有任何疑问,请随时提问:)
【解决方案2】:

对于从 url 输入应用的特定 ActivityFragment,建议使用导航组件中的 深层链接

1.在导航资源文件中添加 URL 深层链接(从属性面板):

<fragment
    android:id="@+id/studentEditorFragment"
    android:name="com.example.roomtest.studentEditor.StudentEditorFragment"
    android:label="StudentEditorFragment"
    tools:layout="@layout/fragment_student_editor">

    <argument
        android:name="studentId"
        app:argType="long" />

    <deepLink
        android:id="@+id/deepLink"
        app:uri="www.example.com/roomTest/{studentId}" />       //here, company domain name followed by application path or argument

</fragment>

2。在AndroidManifest 文件中添加导航属性:

<activity android:name=".MainActivity">                 //【inside the "<activity>" tag】
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <nav-graph android:value="@navigation/navigation" />        //here
</activity>

3.使用网络浏览器或短信中的链接:“www.example.com/roomTest/-1”

  • 要传递多个参数,只需通过“&”连接它们,例如:“www.example.com/roomText/-1&true”(根据提示在 xml 中重新格式化“&”)。

【讨论】:

  • 这不是我要找的。因为我这是一个特定的链接。我需要一些来自我网站的所有链接的东西。
【解决方案3】:

在您的应用中使用 Android 应用链接。如果实施得当,用户将直接移动到应用中的指定目的地。

在这些文档中了解更多信息:

Verify Android App Links

Create an implicit deep link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多