我会尽力帮助您翻译。这段文本的含义是:“我正在发布一个关于应用内计费的逐步过程,我最近在Billing Library 4.0版本发布后进行了测试。”
“Google帮助资源可以在
这里找到。”
“首先要做的事情是:更新你的build.gradle文件。”
dependencies {
def billing_version = "4.0.0"
implementation "com.android.billingclient:billing:$billing_version"
}
现在,进入编程部分:
BillingActivity.java
。
private BillingClient mBillingClient;
submitButton.setOnClickListener(v -> {
mBillingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
Snackbar.make(findViewById(android.R.id.content), "Great!! Purchase Flow Successful! :)", Snackbar.LENGTH_LONG).show();
dismiss();
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
Snackbar.make(findViewById(android.R.id.content), "User Cancelled the Purchase Flow!", Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(findViewById(android.R.id.content), "Error! Purchase Task was not performed!", Snackbar.LENGTH_LONG).show();
}
}
}).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
List<String> skuList = new ArrayList<>();
skuList.add("billing_template_1");
skuList.add("billing_template_2");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult,
List<SkuDetails> skuDetailsList) {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetailsList.get(0))
.build();
int responseCode = mBillingClient.launchBillingFlow(activity, billingFlowParams).getResponseCode();
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
Toast.makeText(BillingActivity.this, "Service Disconnected!", Toast.LENGTH_SHORT).show();
}
});
});
void handlePurchase(Purchase purchase) {
ConsumeParams consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
ConsumeResponseListener listener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, @NonNull String purchaseToken) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
}
}
};
mBillingClient.consumeAsync(consumeParams, listener);
}
private void dismiss() {
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
}
注意:
- 这只是对@Renkuei答案的更新版本。
- 不要忘记
.enablePendingPurchase()
- 不要忘记从您的开发者控制台执行
skuList.add("billing_template_1")
。 因此,billing_template_1
是您的Monetise > Products > In-app products > Prouduct ID的名称