本文說明如何使用 Play Age Signals API 要求年齡信號。
如要要求年齡信號,請使用下列高階工作流程:
(選用) 呼叫會傳回
ApplicableAgeFeaturesResult的getApplicableAgeFeatures()方法。對結果呼叫features(),檢查適用於使用者的年齡相關功能組合。這會傳回:MANDATORY_SHARING:表示使用者必須分享年齡信號。只有當使用者位於法律要求應用程式商店分享年齡資訊的地區時,系統才會傳回這項資訊。如果該使用者也需要驗證年齡,Google Play 會處理年齡驗證,API 則會據此傳回回應。空集合 ([]):為所有其他使用者傳回。
呼叫
requestAgeSignalsAccess(Activity)方法,該方法會傳回ageSignalsStatus。ageSignalsStatus的值可以是SHARED、NOT_SHARED或VERIFICATION_REQUIRED。如果為
ageSignalsStatus == NOT_SHARED:應用程式不會在 API 回應中取得年齡信號。如果是
ageSignalsStatus == SHARED:呼叫checkAgeSignals()方法。如果使用者或家長決定分享年齡信號,您會在 API 回應中收到年齡信號,並可決定如何處理回應。如果為
ageSignalsStatus == VERIFICATION_REQUIRED:使用者尚未驗證年齡,且位於適用管轄區或區域,該地強制要求年齡驗證及分享年齡信號。如要在這些地區取得 Google Play 的年齡信號,請要求使用者前往 Play 商店解決狀態問題。
requestAgeSignalsAccess(Activity) 方法會根據區域是否適用強制分享年齡資訊,傳回不同的值:
- 如果合格使用者位於美國,且所在州別的法律要求應用程式商店向開發人員提供經驗證的年齡資訊,則不會觸發應用程式內提示。使用者造訪 Play 商店應用程式時,系統會要求他們驗證或設定監護功能。請使用
ageSignalsStatus的值判斷驗證狀態:- 如果使用者已完成年齡驗證,或家長監護功能已啟用,
ageSignalsStatus的值會是SHARED。 - 如果使用者尚未驗證年齡或設定監護功能,
ageSignalsStatus的值會是VERIFICATION_REQUIRED。詳情請參閱「美國部分州別即將實施應用程式商店法案,Google Play 將採取因應措施」。
- 如果使用者已完成年齡驗證,或家長監護功能已啟用,
- 如果使用者位於其他地區,且年齡分享設定取決於使用者或家長的選擇:
- 如果使用者的設定是「分享前先詢問」,系統會顯示應用程式內提示。如果使用者同意分享年齡,
ageSignalsStatus的值為SHARED,否則為NOT_SHARED。 - 如果使用者的設定是「一律分享」,系統就不會顯示應用程式內提示,且
ageSignalsStatus的值為SHARED。 - 如果使用者的設定是「Never Share」,系統就不會顯示應用程式內提示,且
ageSignalsStatus的值為NOT_SHARED。 - 如果是受監護使用者,家長可以在 Family Link 應用程式設定中,選擇是否分享孩子的年齡。如果家長選擇分享年齡,
ageSignalsStatus的值會是SHARED,否則會是NOT_SHARED。
- 如果使用者的設定是「分享前先詢問」,系統會顯示應用程式內提示。如果使用者同意分享年齡,
以下範例說明如何要求年齡信號:
Kotlin
// 1. Initialize the AgeSignalsManager (usually in onCreate or class initialization)
val ageSignalsManager = AgeSignalsManagerFactory.create(applicationContext)
// (Optional) Check whether mandatory age sharing applies to the user.
ageSignalsManager.getApplicableAgeFeatures(ApplicableAgeFeaturesRequest.builder().build())
.addOnSuccessListener { featuresResult ->
if (featuresResult.features().contains(ApplicableAgeFeature.MANDATORY_SHARING)) {
// Mandatory age sharing applies to the user.
}
}
// 2. Request or check for age signals access.
// Passing the current Activity allows the Play Store to render the age sharing prompt UI if required.
val accessRequest = AgeSignalsAccessRequest.builder()
.setActivity(this)
.build()
ageSignalsManager.requestAgeSignalsAccess(accessRequest)
.addOnSuccessListener { accessResult ->
if (accessResult.ageSignalsStatus() == AgeSignalsStatus.SHARED) {
// The user (or parent) has agreed to share age range, or is in an eligible auto-share region.
// Retrieve the actual age signals.
retrieveAgeSignals(ageSignalsManager)
} else {
// Age signals are not shared (user didn't share age range, parent rejected the request, or not eligible).
}
}
.addOnFailureListener { exception ->
// Handle API/Play Store connection and system errors
handleAgeSignalsError(exception)
}
private fun retrieveAgeSignals(manager: AgeSignalsManager) {
// 3. Perform the actual age signals query once sharing is active.
manager.checkAgeSignals(AgeSignalsRequest.builder().build())
.addOnSuccessListener { ageSignalsResult ->
val installId = ageSignalsResult.installId()
val ageLower = ageSignalsResult.ageLower()
val ageUpper = ageSignalsResult.ageUpper()
val significantChangeDate = ageSignalsResult.significantChangeApprovalDate()
val ageRangeSource = ageSignalsResult.ageRangeSource()
if (ageLower != null) {
if (ageUpper != null) {
// The user is in a specific closed age range [ageLower, ageUpper] (e.g. [13, 15])
} else {
// The user is in the highest open-ended age band [ageLower, null] (e.g. [18, null])
}
} else {
// Both bounds are null: The user is not sharing their age
}
}
.addOnFailureListener { exception ->
handleAgeSignalsError(exception)
}
}
Java
// 1. Initialize the AgeSignalsManager (usually in onCreate or class initialization)
AgeSignalsManager ageSignalsManager = AgeSignalsManagerFactory.create(getApplicationContext());
// (Optional) Check whether mandatory age sharing applies to the user.
ageSignalsManager.getApplicableAgeFeatures(ApplicableAgeFeaturesRequest.builder().build())
.addOnSuccessListener(featuresResult -> {
if (featuresResult.features().contains(ApplicableAgeFeature.MANDATORY_SHARING)) {
// Mandatory age sharing applies to the user.
}
});
// 2. Request or check for age signals access.
// Passing the current Activity allows the Play Store to render the age sharing prompt UI if required.
AgeSignalsAccessRequest accessRequest = AgeSignalsAccessRequest.builder()
.setActivity(this)
.build();
ageSignalsManager.requestAgeSignalsAccess(accessRequest)
.addOnSuccessListener(accessResult -> {
Integer status = accessResult.ageSignalsStatus();
if (status == AgeSignalsStatus.SHARED) {
// The user (or parent) has agreed to share age range, or is in an eligible auto-share region.
// Retrieve the actual age signals.
retrieveAgeSignals(ageSignalsManager);
} else {
// Age signals are not shared (user didn't share age range, parent rejected the request, or not eligible).
}
})
.addOnFailureListener(exception -> {
// Handle API/Play Store connection and system errors
});
private void retrieveAgeSignals(AgeSignalsManager manager) {
// 3. Perform the actual age signals query once sharing is active.
manager.checkAgeSignals(AgeSignalsRequest.builder().build())
.addOnSuccessListener(ageSignalsResult -> {
String installId = ageSignalsResult.installId();
Integer ageLower = ageSignalsResult.ageLower();
Integer ageUpper = ageSignalsResult.ageUpper();
Date significantChangeDate = ageSignalsResult.significantChangeApprovalDate();
@AgeRangeSource Integer ageRangeSource = ageSignalsResult.ageRangeSource();
if (ageLower != null) {
if (ageUpper != null) {
// The user is in a specific closed age range [ageLower, ageUpper] (e.g. [13, 15])
} else {
// The user is in the highest open-ended age band [ageLower, null] (e.g. [18, null])
}
} else {
// Both bounds are null: The user is not sharing their age (e.g. they are a verified adult)
}
})
.addOnFailureListener(exception -> {
handleAgeSignalsError(exception);
});
}
程式碼重點
getApplicableAgeFeatures()方法會檢查哪些年齡相關功能 (例如ApplicableAgeFeature.MANDATORY_SHARING) 適用於使用者。requestAgeSignalsAccess(Activity)方法會對使用者目前的年齡信號和年齡信號分享狀態執行封鎖檢查。- 呼叫
requestAgeSignalsAccess(Activity)後,Play 只會向不受監管的使用者顯示內建的應用程式內提示。受監護使用者的家長可以選擇分享孩子的年齡,方法是在 Family Link 應用程式中管理年齡分享設定。 - 如果使用者關閉或拒絕分享年齡,系統會顯示幾次應用程式內提示,然後才會停止顯示。
checkAgeSignals()方法會取得 API 回應中的年齡信號值。這個方法會傳回ageRangeSource、ageUpper、ageLower和其他重大變更值。