android - 无法理解如何在应用程序中打开简单的 TWA(使用 AndroidX)

标签 android nightmare trusted-web-activity

我正在尝试在我的应用程序中打开 TWA 并研究了 2 天。

我已经设法创建了一个 TWA 应用程序,不用大惊小怪,只需编辑 list 和其他一些东西。

现在我需要拥有自己的应用程序 - 假设该应用程序首先具有启动屏幕 Activity ,然后在应用程序内打开 TWA。
例如,我可以通过简单的闪屏 Activity 在我的应用程序中启动 TWA 吗?

我确实尝试过使用 CustomTabs方式,但它说它已被弃用并使用 TrustedWebActivityIntentBuilder相反,但是有 0,我重复一遍,关于如何使用它的零文档!

Android 开发文档很糟糕。除其他外,文档指针已过时。 (阅读他们 channel 上的视频,链接到对 video 本身讨论的内容不再有效的指南)

我找到的最接近的东西是 sample project .这使用了数量惊人的不推荐使用的东西,使得将该方法适应我的应用程序完全无用。它还利用了为该项目创建的无数自定义类/助手,这让我经历了一场永无止境的马拉松式的复制粘贴它们中的每一个,只是为了发现其中还有更多需要复制到项目。

最佳答案

在我看来,有一种更简单的方法。

首先:在 AndroidManifest.xml 中声明您的 TWA Activity ,如下所示。请注意,默认情况下它不会启动,因为它不处理 android.intent.action.MAIN Intent ,因此您可以实现自己的主要 Activity 。

    <activity
        android:name="com.google.androidbrowserhelper.trusted.LauncherActivity">

        <!-- Edit android:value to change the url opened by the TWA -->
        <meta-data
            android:name="android.support.customtabs.trusted.DEFAULT_URL"
            android:value="https://YOUR_SITE_URL" />

        <!--
          This intent-filter allows the TWA to handle Intents to open
          YOUR_SITE_URL
        -->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE"/>

            <!-- Edit android:host to handle links to the target URL-->
            <data
                android:scheme="https"
                android:host="YOUR_SITE_URL"/>
        </intent-filter>
    </activity>

第二:在您的代码中某处以这样的 Intent 启动 TWA Activity 。如果您愿意,您还可以在 Intent 中传递站点 url。
Intent intent = new Intent(this, com.google.androidbrowserhelper.trusted.LauncherActivity.class);
intent.setData(Uri.parse("ANOTHER_SITE_URL"));
startActivity(intent);

还要注意将 TWA 与 AndroidX 一起使用所需的依赖项:
implementation 'androidx.browser:browser:1.0.0'
implementation 'com.github.GoogleChrome:android-browser-helper:ff8dfc4ed3d4133aacc837673c88d090d3628ec8'

关于android - 无法理解如何在应用程序中打开简单的 TWA(使用 AndroidX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57741990/

相关文章:

android - 按钮文本中的多种字体大小

android - 什么是最强大的 Android HTTP 库?

android - 压缩图像时出现空指针异常

progressive-web-apps - 如何删除 TWA 中的底部导航栏?

google-chrome - 受信任的 Web 事件 - Intranet/私有(private) Web 应用程序的数字 Assets 链接验证似乎失败

android - 我在哪里可以下载 native Android .ogg 文件?

javascript - Nightmare JS返回页面状态码

javascript - 使用nightmare.js发出各种请求

javascript - Ubuntu中的NodeJS不会将console.log()打印到终端

angular - 我已经遵循了可信网络事件的所有步骤,但使用 chrome dev 仍然可以看到地址栏。如何在可信网络事件中获取 webview?