[英]Cannot understand how to open a simple TWA inside an app (using AndroidX)
我正在嘗試在我的應用程序中打開一個 TWA 並且已經研究了 2 天。
我已經設法創建了一個 TWA 應用程序,不用大驚小怪,只需編輯清單和其他一些東西。
現在我需要擁有自己的應用程序 - 假設該應用程序首先有一個閃屏活動,然后在應用程序內打開 TWA。 例如,我可以通過簡單的啟動屏幕活動在我的應用程序中啟動 TWA 嗎?
我確實嘗試過使用CustomTabs
方式,但它說它已被棄用,而是使用TrustedWebActivityIntentBuilder
,但有 0,我再說一遍,關於如何使用它的零文檔!
Android 開發文檔很糟糕。 除其他外,文檔指針已過時。 (閱讀他們頻道上的視頻,這些視頻鏈接到對視頻本身討論的內容不再有效的指南)
我發現最接近的是這個示例項目。 這使用了大量已棄用的東西,使將該方法適應我的應用程序完全無用。 它還利用了為該項目創建的無數自定義類/幫助程序,讓我進行了一場永無止境的馬拉松式復制粘貼每一個只是為了發現在那個里面還有更多需要復制到項目。
在我看來,有一種更簡單的方法。
首先:在 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'
從現有活動啟動受信任的 Web 活動時,推薦的方法是使用來自android-browser-helper
TwaLauncher
。 這個用例有一個演示,但實現是:
build.gradle
導入android-browser-helper
:dependencies {
...
implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.0.0'
}
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);
}
與其他方法相比:
使用LauncherActivity
:使用LauncherActivity
增加了一個額外的間接級別,這會在從現有 Activity 啟動受信任的 Web 活動時引入延遲(例如:Activity X 啟動 LauncherActivity,然后啟動受信任的 Web 活動)。
直接使用androidx.browser
:這種方法沒有任何問題,但TwaLauncher
封裝了幾乎所有需要處理的東西。
經過一如往常的反復試驗,我設法在應用程序內啟動了 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);
}
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.