在 Android SDK 中检查订阅状态
安装 AI Agent 工具
在你的 AI 编程工具中完成整个 Adapty SDK 集成。 了解更多
claude plugin marketplace add adaptyteam/adapty-sdk-integration-skill
claude plugin install adapty-sdk-integration@adapty
gh skill install adaptyteam/adapty-sdk-integration-skill
gemini skills install https://github.com/adaptyteam/adapty-sdk-integration-skill
git clone https://github.com/adaptyteam/adapty-sdk-integration-skill.git
cp -r adapty-sdk-integration-skill/skills/adapty-sdk-integration ~/.agents/skills/
可安装到任何支持技能的 AI 工具。之后更新请运行 npx skills update。
npx skills add adaptyteam/adapty-sdk-integration-skill
安装后,在你的项目中运行该技能:
/adapty-sdk-integration
要决定用户是否可以访问付费内容或查看付费墙,您需要检查用户画像中的访问等级。
本文介绍如何访问用户画像状态,以决定向用户展示什么内容——是显示付费墙还是授予付费功能的访问权限。
获取订阅状态
当您决定是否向用户显示付费墙或付费内容时,需要检查其用户画像中的访问等级。您有两种选择:
- 如果需要立即获取最新的用户画像数据(例如在应用启动时)或想要强制更新,请调用
getProfile。 - 设置自动用户画像更新,以保存一份本地副本,该副本会在订阅状态发生变化时自动刷新。
获取用户画像
获取订阅状态最简单的方法是使用 getProfile 方法访问用户画像:
Adapty.getProfile { result ->
when (result) {
is AdaptyResult.Success -> {
val profile = result.value
// check the access
}
is AdaptyResult.Error -> {
val error = result.error
// handle the error
}
}
} Adapty.getProfile(result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
// check the access
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
// handle the error
}
}); 监听订阅更新
要在应用中自动接收用户画像更新:
- 使用
Adapty.setOnProfileUpdatedListener()监听用户画像变化——每当用户的订阅状态发生变化时,Adapty 会自动调用此方法。 - 在此方法被调用时存储更新后的用户画像数据,以便在整个应用中使用,而无需发起额外的网络请求。
class SubscriptionManager {
private var currentProfile: AdaptyProfile? = null
init {
// Listen for profile updates
Adapty.setOnProfileUpdatedListener { profile ->
currentProfile = profile
// Update UI, unlock content, etc.
}
}
// Use stored profile instead of calling getProfile()
fun hasAccess(): Boolean {
return currentProfile?.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true
}
} public class SubscriptionManager {
private AdaptyProfile currentProfile;
public SubscriptionManager() {
// Listen for profile updates
Adapty.setOnProfileUpdatedListener(profile -> {
this.currentProfile = profile;
// Update UI, unlock content, etc.
});
}
// Use stored profile instead of calling getProfile()
public boolean hasAccess() {
if (currentProfile == null) {
return false;
}
AdaptyAccessLevel premiumAccess = currentProfile.getAccessLevels().get("YOUR_ACCESS_LEVEL");
return premiumAccess != null && premiumAccess.isActive();
}
} Adapty 会在应用启动时自动调用用户画像更新监听器,即使设备处于离线状态,也能提供缓存的订阅数据。
将用户画像与付费墙逻辑关联
当您需要立即决定是否显示付费墙或授予付费功能访问权限时,可以直接检查用户的用户画像。此方法适用于应用启动、进入付费专区或显示特定内容之前等场景。
private fun initializePaywall() {
loadPaywall { paywallView ->
checkAccessLevel { result ->
when (result) {
is AdaptyResult.Success -> {
if (!result.value && paywallView != null) {
setContentView(paywallView) // Show paywall if no access
}
}
is AdaptyResult.Error -> {
if (paywallView != null) {
setContentView(paywallView) // Show paywall if access check fails
}
}
}
}
}
}
private fun checkAccessLevel(callback: ResultCallback<Boolean>) {
Adapty.getProfile { result ->
when (result) {
is AdaptyResult.Success -> {
val hasAccess = result.value.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true
callback.onResult(AdaptyResult.Success(hasAccess))
}
is AdaptyResult.Error -> {
callback.onResult(AdaptyResult.Error(result.error))
}
}
}
} private void initializePaywall() {
loadPaywall(paywallView -> {
checkAccessLevel(result -> {
if (result instanceof AdaptyResult.Success) {
boolean hasAccess = ((AdaptyResult.Success<Boolean>) result).getValue();
if (!hasAccess && paywallView != null) {
setContentView(paywallView); // Show paywall if no access
}
} else if (result instanceof AdaptyResult.Error) {
if (paywallView != null) {
setContentView(paywallView); // Show paywall if access check fails
}
}
});
});
}
private void checkAccessLevel(ResultCallback<Boolean> callback) {
Adapty.getProfile(result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
AdaptyAccessLevel premiumAccess = profile.getAccessLevels().get("YOUR_ACCESS_LEVEL");
boolean hasAccess = premiumAccess != null && premiumAccess.isActive();
callback.onResult(AdaptyResult.success(hasAccess));
} else if (result instanceof AdaptyResult.Error) {
callback.onResult(AdaptyResult.error(((AdaptyResult.Error) result).getError()));
}
});
} 后续步骤
现在您已经了解如何追踪订阅状态,接下来请学习如何使用用户画像,以确保用户能够访问他们已付费的内容。