问题描述
在 android 中使用计费库并购买订阅而不是应用内产品(消耗品-非消耗品),将 .setType(SkuType.SUBS)
添加到 BillingFlowParams
如下:
val billingFlowParams = BillingFlowParams
.newBuilder()
.setType(SkuType.SUBS)
.setSkuDetails(skuDetailsList!![i])
.build()
但是,.setType(SkuType.SUBS)
向我展示了未解析的引用:setType
可能是什么问题? 如何启动应用内订阅的购买流程?
解决方法
试试BillingClient.SkuType.SUBS
您可以启动如下购买流程
billingClient.launchBillingFlow(this,billingFlowParams).getResponseCode();
如果我们更彻底,
-顺便说一句,它是在java中
首先,您必须在类中实现 PurchasesUpdatedListener,AcknowledgePurchaseResponseListener
并覆盖 onPurchasesUpdated
和 onAcknowledgePurchaseResponse
方法。
然后初始化计费客户端
private void initBilling() {
billingClient = BillingClient.newBuilder(this)
.enablePendingPurchases()
.setListener(this)
.build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS,(billingResult1,purchaseHistoryRecordList) -> {
Log.i("SKURESLUT",purchaseHistoryRecordList.toString());
});
}
}
@Override
public void onBillingServiceDisconnected() {
}
});
}
然后您可以将所需的包id传递给以下方法并启动billingFlow,
private void getBillingPackage(String id) {
List<String> productIdsList = new ArrayList<>();
productIdsList.add(id);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(productIdsList).setType(BillingClient.SkuType.SUBS);
billingClient.querySkuDetailsAsync(params.build(),(result,skuDetailsList) -> {
if (!skuDetailsList.isEmpty()) {
mSkuDetails.addAll(skuDetailsList);
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetailsList.get(0))
.build();
int responseCode = billingClient.launchBillingFlow(this,billingFlowParams).getResponseCode();
} else {
Toast.makeText(this,"Requested package not available",Toast.LENGTH_LONG).show();
}
Log.e("SKURESLUT",result.toString());
});
}
然后在 onPurchaseUpdated 方法将在计费流程完成或取消时调用。您可以在此处确认购买
@Override
public void onPurchasesUpdated(BillingResult billingResult,@Nullable List<Purchase> purchases) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
Log.e("Purchase:",purchase.toString());
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
currentPurchase = purchase;
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams,this);
}
}
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
Toast.makeText(this,"Purchase Cancelled",Toast.LENGTH_LONG).show();
} else {
// Handle any other error codes.
}
}
然后你可以像下面这样获得AcknowledgePurchase
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
//Purchase Acknowledged and verified
} else {
Toast.makeText(this,"Purchase Failed",Toast.LENGTH_LONG).show();
}
Log.e("Acknowledged Purchase:",billingResult.toString());
}