本指南說明如何使用 Kotlin 或 Java,在應用程式中整合應用程式內安裝提示。
設定開發環境
Play 應用程式內安裝提示程式庫是 Google Play Core Library 的一部分。如要使用這個程式庫,請加入下列 Gradle 依附元件:
// In your app's build.gradle.kts file:
...
dependencies {
implementation("com.google.android.play:crossdeviceprompt:0.0.1")
...
}
顯示跨裝置安裝提示
在應用程式流程中找出最佳時機,提示使用者在其他裝置上安裝應用程式 (例如使用者將手機上的影片投放到電視時)。當應用程式到達其中一個時間點時,請執行下列步驟:
- 建立
CrossDevicePromptInstallationRequest。 - 使用
CrossDevicePromptManager建立要求工作,並接受要求做為參數。 - 使用產生的
CrossDevicePromptInfo物件和launchPromptFlow(),向使用者顯示提示。
如果在取得 CrossDevicePromptInfo 或顯示提示時發生錯誤,系統會擲回例外狀況。
val crossDevicePromptManager = CrossDevicePromptManagerFactory.create(activity)
val request = CrossDevicePromptInstallationRequest.create()
try {
val info = crossDevicePromptManager.requestInstallationPromptFlow(request).await()
crossDevicePromptManager.launchPromptFlow(activity, info).await()
} catch (e: CrossDevicePromptException) {
Log.e(TAG, "Cross-device prompt failed with error: ${e.errorCode}", e)
}
如要驗證導入狀態,請參閱「測試應用程式內安裝提示」。