本指南的第三部分將著重說明「自訂分頁服務」,以及在應用程式中使用這項服務的原因,可以帶來更優質的使用者體驗:
- 立即開啟外部內容:使用
warmup()
會啟動瀏覽器程序,在使用者點選連結前就在背景啟動,且開啟連結時可節省多達 700 毫秒。mayLaunchUrl()
會預先擷取網頁。同時使用這兩種 API 可讓網頁立即載入,大幅提升「自訂分頁」整合功能的使用者體驗。 - 更妥善地處理最小化「自訂」分頁:連上「自訂分頁」服務,並在啟動「自訂分頁」時使用相同的
CustomTabSession
,Chrome 就能在開啟新分頁之前移除先前最小化的自訂分頁,提供更加一致的使用者體驗。
必要步驟如下:
- 使用
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 應用程式中開啟連結
在 Android 中,網址可由 Android 應用程式處理。舉例來說,假設使用者已安裝 Facebook 應用程式,並點選某個 Facebook 貼文的連結,通常會偏好使用 Facebook 應用程式 (而非瀏覽器) 開啟連結。
根據預設,如果已安裝自訂分頁,系統會在對應的 Android 應用程式中開啟連結。不過,CustomTabsServiceConnection
建立完成後,此行為就會停止運作,且所有網址都會改為在「自訂分頁」中開啟。為了改善使用者體驗,建議您使用下列程式碼重新啟用這項行為:
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.setSendToExternalDefaultHandlerEnabled(true)
.build();
接下來:瞭解如何調整自訂分頁體驗的大小。