Sha*_*dow 5 android nightmare trusted-web-activity
我正在尝试在我的应用程序中打开一个 TWA 并且已经研究了 2 天。
我已经设法创建了一个 TWA 应用程序,不用大惊小怪,只需编辑清单和其他一些东西。
现在我需要拥有自己的应用程序 - 假设该应用程序首先有一个闪屏活动,然后在应用程序内打开 TWA。例如,我可以通过简单的启动屏幕活动在我的应用程序中启动 TWA 吗?
我确实尝试过使用CustomTabs方式,但它说它已被弃用并改为使用TrustedWebActivityIntentBuilder,但我重复一遍,关于如何使用它的零文档!
Android 开发文档很糟糕。除其他外,文档指针已过时。(阅读他们频道上的视频,这些视频链接到对视频本身讨论的内容不再有效的指南)
我发现最接近的是这个示例项目。这使用了大量已弃用的东西,使将该方法适应我的应用程序完全无用。它还利用了为该项目创建的无数自定义类/帮助程序,让我进行了一场永无止境的马拉松式复制粘贴每一个只是为了发现在那个里面还有更多需要复制到项目。
从现有活动启动受信任的 Web 活动时,建议的方法是使用TwaLauncher, from android-browser-helper。这个用例有一个演示,但实现是:
android-browser-helper到build.gradle:dependencies {
...
implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.0.0'
}
Run Code Online (Sandbox Code Playgroud)
dependencies {
...
implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.0.0'
}
Run Code Online (Sandbox Code Playgroud)
与其他方法相比:
使用LauncherActivity:使用LauncherActivity增加了额外的间接级别,这会在从现有活动启动受信任的 Web 活动时引入延迟(例如:活动 X 启动 LauncherActivity,而 LauncherActivity 又启动受信任的 Web 活动)。
直接使用androidx.browser:这种方法没有任何问题,但TwaLauncher已经封装了处理该问题所需的几乎所有内容。
在我看来,有一种更简单的方法。
首先:在 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>
Run Code Online (Sandbox Code Playgroud)
第二:在您的代码中的某处以这样的意图启动 TWA 活动。如果您愿意,您还可以在意图中传递站点 url。
Intent intent = new Intent(this, com.google.androidbrowserhelper.trusted.LauncherActivity.class);
intent.setData(Uri.parse("ANOTHER_SITE_URL"));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
另请注意将 TWA 与 AndroidX 结合使用所需的依赖项:
implementation 'androidx.browser:browser:1.0.0'
implementation 'com.github.GoogleChrome:android-browser-helper:ff8dfc4ed3d4133aacc837673c88d090d3628ec8'
Run Code Online (Sandbox Code Playgroud)
经过大量的尝试和错误之后,我成功地在应用程序内启动了 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);
}
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2616 次 |
| 最近记录: |