无法理解如何在应用程序中打开简单的TWA(使用AndroidX) [英] Cannot understand how to open a simple TWA inside an app (using AndroidX)
问题描述
我正在尝试在我的应用程序中打开TWA,并进行了2天的研究.
I am trying to open a TWA inside my app and have researched for 2 days.
我已经设法创建了一个TWA应用程序,不必大惊小怪,只需编辑清单即可.
I've already managed to create a single TWA app, no fuss, just edit the manifest and a few things more.
现在我需要拥有自己的应用程序-假设该应用程序首先具有启动屏幕活动,然后在应用程序内部打开TWA. 例如,是否可以通过简单的启动屏幕活动在我的应用程序内启动TWA?
Now I need to have my own app - let's say the app has a splash screen activity at first which then opens the TWA inside the app. Can I launch a TWA inside my app through a simple splash screen activity, for example?
我确实尝试使用CustomTabs
的方式,但是它说它已被弃用,而改用TrustedWebActivityIntentBuilder
,但是我重复了0,关于如何使用它的零文档!
I did try to use CustomTabs
way, but it says it is deprecated and to use TrustedWebActivityIntentBuilder
instead, but there is 0, I repeat, ZERO documentation on how to use that!
Android开发文档太可怕了.除其他事项外,文档指针已过时. (阅读其频道上的视频,这些视频链接到对
Android development documentation is horrible. Among other things, the documentation pointers are out-dated. (Read videos on their channel linking to guides that are no longer valid for what is discussed in the video itself)
我找到的最接近的东西是示例项目.这使用了大量令人讨厌的东西,使该方法适应我的应用程序变得完全无用.它还利用了仅为该项目创建的无数个自定义类/助手,这使我进行了无休止的复制粘贴马拉松竞赛,以发现其中每个对象都需要复制转到项目.
The closest thing I found was this sample project. This uses a shocking number of deprecated things rendering the adaptation of that method into my app completely useless. It also makes use of a countless number of custom Classes/Helpers created just for that project, leading me to a never ending marathon of copy-pasting each one of them just to find out that inside that one there are more that need to be copied over to the project.
推荐答案
经过一再的尝试和错误,我设法在应用程序内部启动了TWA.请注意,这与WebView不同,它只是在应用程序堆栈上启动活动.
After a lot of trial and error as always I've managed to launch a TWA inside the app. Note that this is not like a WebView, it merely starts an activity on your app's stack.
以下是我的活动的代码:
The following is in my Activity's code:
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);
}
};
}
这篇关于无法理解如何在应用程序中打开简单的TWA(使用AndroidX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!