本指南的第三部分著重於透過 warmup()
加快瀏覽器啟動速度,以及透過 mayLaunchUrl
() 預先擷取網頁。開啟連結時,啟動瀏覽器程序最多可省下 700 毫秒。透過 mayLaunchUrl
預先算繪內容,可立即開啟外部內容。同時使用這兩種 API 可大幅改善「自訂分頁」整合作業的使用者體驗,因此我們極力推薦您使用。
必要步驟如下:
- 透過
CustomTabsClient.getPackageName(...)
檢查 (如果預設瀏覽器支援自訂分頁)。如果是,請透過CustomTabsClient.bindCustomTabsService()
繫結至 CustomTabsService。 連線至 CustomTabsService 後,請在
CustomTabsServiceConnection.onCustomTabsServiceConnected()
回呼中執行以下動作:a. 透過
CustomTabsClient.warmup()
暖機瀏覽器程序。b. 透過CustomTabsClient.newSession()
建立新的CustomTabsSession
。視需要預先擷取使用者可能透過
CustomTabsSession.mayLaunchUrl()
造訪的網頁。啟動新的「Custom」分頁時,透過建構函式
new CustomTabsIntent.Builder(session)
將CustomTabsSession
傳送至 CustomTabsIntent.Builder。
如果您的應用程式指定 Android API 級別 30 為目標,CustomTabsClient.getPackageName(...)
會要求您在 Android 資訊清單中加入查詢區段,並宣告符合支援自訂分頁瀏覽器的意圖篩選器。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
…
<queries>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
</manifest>
以下是如何連結「自訂分頁」服務的完整範例:
private CustomTabsClient mClient;
private CustomTabsSession mSession;
private CustomTabsServiceConnection mConnection = new CustomTabsServiceConnection() {
@Override
public void onCustomTabsServiceConnected(
@NonNull ComponentName name,
@NonNull CustomTabsClient client
) {
mClient = client;
// Warm up the browser process
mClient.warmup(0 /* placeholder for future use */);
// Create a new browser session
mSession = mClient.newSession(new CustomTabsCallback());
// Pre-render pages the user is likely to visit
// you can do this any time while the service is connected
mSession.mayLaunchUrl(Uri.parse("https://developers.android.com"), null, null);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mClient = null;
mSession = null;
}
};
private void bindCustomTabService(Context context) {
// Check for an existing connection
if (mClient != null) {
// Do nothing if there is an existing service connection
return;
}
// Get the default browser package name, this will be null if
// the default browser does not provide a CustomTabsService
String packageName = CustomTabsClient.getPackageName(context, null);
if (packageName == null) {
// Do nothing as service connection is not supported
return;
}
CustomTabsClient.bindCustomTabsService(context, packageName, mConnection);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
…
bindCustomTabService(this);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "https://developers.android.com";
CustomTabsIntent intent = new CustomTabsIntent.Builder(mSession)
.build();
intent.launchUrl(MainActivity.this, Uri.parse(url));
}
});
}
在原生應用程式中開啟網頁
在 Android 上,網址可由原生應用程式處理。舉例來說,假設使用者已安裝 Facebook 應用程式,並點選某個 Facebook 貼文的連結,通常會偏好使用 Facebook 應用程式 (而非瀏覽器) 開啟連結。
根據預設,如果已安裝「自訂分頁」,系統會在對應的原生應用程式中開啟連結。不過,CustomTabsServiceConnection
建立完成後,此行為就會停止運作,且所有網址都會改為在「自訂分頁」中開啟。為了改善使用者體驗,建議您使用下列程式碼重新啟用這項行為:
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.setSendToExternalDefaultHandlerEnabled(true)
.build();
接下來:瞭解如何調整自訂分頁體驗的大小。