6

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

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

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

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

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

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

4

3 回答 3

5

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

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

    <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>

第二:在您的代码中某处以这样的意图启动 TWA 活动。如果您愿意,您还可以在意图中传递站点 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'
于 2019-09-23T20:24:03.330 回答
3

从现有 Activity 启动 Trusted Web Activity 时,推荐的方法是使用TwaLauncher, from android-browser-helper。这个用例有一个演示,但实现是:

  1. 导入:android-browser-helper_build.gradle
dependencies {
    ...
    implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.0.0'    
}
  1. 从 Activity 启动 Trusted Web Activity:
public void launchTwa(Uri uri) {
    TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(uri)
        .setNavigationBarColor(Color.RED) // Use the builder to customise.
        .setToolbarColor(Color.BLUE);

    TwaLauncher launcher = new TwaLauncher(this);
    launcher.launch(builder, null, null);
}

与其他方法相比:

Using LauncherActivity: UsingLauncherActivity增加了一个额外的间接级别,这将在从现有 Activity 启动 Trusted Web Activity 时引入延迟(例如:Activity X 启动 LauncherActivity,而后者又启动 Trusted Web Activity)。

直接使用androidx.browser:这种方法没有错,但TwaLauncher封装了几乎所有需要处理的东西。

于 2020-10-15T14:00:20.430 回答
0

像往常一样经过大量试验和错误后,我设法在应用程序内启动了 TWA。请注意,这不像 WebView,它只是在您的应用程序堆栈上启动一个活动。

以下是我的活动代码:

final static String PACKAGE_NAME = "com.android.chrome";
final static String BASE_URL = "https://your.website.com";
CustomTabsClient mClient;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_launcher);

    CustomTabsClient.bindCustomTabsService(this, PACKAGE_NAME, getConnection(BASE_URL));

}

private CustomTabsServiceConnection getConnection(final String url) {

    return new CustomTabsServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mClient = null;
        }

        @Override
        public void onCustomTabsServiceConnected(
            ComponentName name,
            CustomTabsClient client
        ) {

            final Intent intent;
            final CustomTabsSession customTabsSession;
            final TrustedWebActivityIntentBuilder intentBuilder;

            mClient = client;
            mClient.warmup(0);

            if ((customTabsSession = mClient.newSession(new CustomTabsCallback())) == null) {
                return;
            }

            intentBuilder = new TrustedWebActivityIntentBuilder(Uri.parse(url))
                .setToolbarColor(Color.parseColor("#ffffff"))
                .setNavigationBarColor(Color.parseColor("#ffffff"));

            intent = intentBuilder.build(customTabsSession);

            startActivity(intent);

        }

    };

}
于 2019-09-01T11:26:05.777 回答