android - 如何检查项目(非消耗性应用内购买)是否已在 Android Studio 中以编程方式拥有或未拥有?
问题描述
我想知道我是否可以检查已经拥有的非消耗性应用内购买。请帮我。
解决方案
是的,您可以non-consumable
In-App Purchase
使用 android BillingClient检查是否已经拥有
您必须dependency
在您的build.gradle
implementation 'com.android.billingclient:billing:1.2'
permission
并在您的AndroidManifest.xml
文件中使用以下内容。
<uses-permission android:name="com.android.vending.BILLING" />
您可以in-app purchase
通过queryPurchaseHistoryAsync
在mBillingClient
.
这是我如何在我的项目中实现它的示例。希望它可以帮助你。
private BillingClient mBillingClient;
private SkuDetailsParams.Builder params;
private SkuDetails skuDetails;
protected void getPurchaseHistory() {
mBillingClient = BillingClient.newBuilder(this).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
Log.d(TAG, "onBillingSetupFinished ");
mBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS,
new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(@BillingClient.BillingResponse int responseCode,
List<Purchase> purchasesList) {
if (responseCode == BillingClient.BillingResponse.OK
&& purchasesList != null) {
boolean productFound = false;
for (Purchase purchase : purchasesList) {
// check is user purchased your product or not
if (purchase.getSku().equals("your in-app product id")) {
//success! your user has bought your In-App product. Woohoo!
productFound = true;
// you can check user subscription details by using purchase token
checkSubscription(purchase.getPurchaseToken());
prefs().setPurchaseToken(purchase.getPurchaseToken());
}
}
if (!productFound) {
// user not purchase your product yet.
prefs().setIsSubscribed(false);
Log.d(TAG, "product not found");
}
} else {
prefs().setIsSubscribed(false);
Log.d(TAG, "Purchase history response not ok");
}
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.d(TAG, "onBillingServiceDisconnected ");
}
});
}
推荐阅读
- python - 创建一个列,根据条件删除不需要的字符串部分
- python - 使用 matplotlib 时,AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'
- python - 以特定模式重复字符串而不切片
- java - 如何使用 React Native 关闭像 modal 这样的组件?
- angular - Typescript:通用类型检查和子可用性
- java - 如何传递 id 是否正确?我不明白错误?
- javascript - Client.db 未定义(MongoDB 后端 API)错误
- spring-boot - 资源服务器的 Spring 集成测试(基于 spring-cloud-starter-oauth2)
- ios - 如何使用 for 循环获取 UNNotificationRequest 数组以填充 SwiftUI 列表视图?
- wordpress - ansible playbook 使用 wp-cli 安装 WordPress 时出错