1.build.gradle
implementation("com.android.billingclient:billing:8.0.0")
implementation 'com.google.guava:guava:31.1-android'
implementation 'com.google.android.gms:play-services-base:18.2.0'
2.写入代码
/// ///////////////////////////////谷歌充值/////////////////////////////////////////////////////////////
//----------------------1.谷歌连接服务器-------------------------------
private BillingClient billingClient;
private static final long RECONNECT_TIMER_START_MILLISECONDS = 1L * 1000L;
private static final long RECONNECT_TIMER_MAX_TIME_MILLISECONDS = 1000L * 60L * 15L; // 15 mins
private long reconnectMilliseconds = RECONNECT_TIMER_START_MILLISECONDS;
private boolean billingSetupComplete = false;
private static final Handler handler = new Handler(Looper.getMainLooper());
protected static Handler uiHandler = new Handler(Looper.getMainLooper());
private static Map<String, SkuDetails> skuDetailsLiveDataMap = new HashMap<>();
private String[] cacheRequestList;
private String buyProductId;
private static Map<String, ProductDetails> productDetailsMap = new HashMap<>();
private boolean isConsumable;
//----------------------1.谷歌连接服务器-------------------------------
protected void BillingClientInit() {
System.out.print("-------0.连接服务器-----" + "\n");
PendingPurchasesParams params = PendingPurchasesParams.newBuilder()
.enableOneTimeProducts()
.build();
billingClient = BillingClient.newBuilder(this)
.setListener(this)
.enablePendingPurchases(params)
.enableAutoServiceReconnection() // Add this line to enable reconnection
.build();
billingClient.startConnection(this);
}
@Override
public void onBillingServiceDisconnected() {
billingSetupComplete = false;
retryBillingServiceConnectionWithExponentialBackoff();
}
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
int responseCode = billingResult.getResponseCode();
String debugMessage = billingResult.getDebugMessage();
System.out.print("-------1.连接服务器状态\n");
switch (responseCode) {
case BillingClient.BillingResponseCode.OK:
reconnectMilliseconds = RECONNECT_TIMER_START_MILLISECONDS;
billingSetupComplete = true;
System.out.print("-------2.连接服务器成功 onBillingSetupFinished " + debugMessage + "(" + GetResponseText(responseCode) + ")" + "\n");
// new Handler(Looper.getMainLooper()).postDelayed(() -> {
// GoogleOnBuyProduct("3401101", false);
// }, 2000);
break;
case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
System.out.print("-------3.连接服务器失败onBillingSetupFinished " + debugMessage + "(" + GetResponseText(responseCode) + ")" + "\n");
break;
default:
retryBillingServiceConnectionWithExponentialBackoff();
break;
}
}
//----------------------------------------------------2.调用商品id----------------------------------------------------------------------------
public static void GoogleOnBuyProduct(String productId, boolean isConsumable1) {
System.out.print("-------2.开始购买1 OnBuyProduct------ productId:" + productId + ",isConsumable:" + isConsumable1 + "\n");
uiHandler.post(new Runnable() {
@Override
public void run() {
try {
activity.OnBuyProduct1(productId, true);
} catch (Exception e) {
System.out.print("-------2.开始购买999 BuyProduct数据传输错误:" + e.getMessage() + "\n");
}
}
});
}
private void OnBuyProduct1(String productId, boolean isConsumable) {
if (!billingSetupComplete) {
Log.e("Billing", "Billing client not ready");
return;
}
System.out.print("-------2.开始购买2 OnBuyProduct1------ productId:" + productId + ",isConsumable:" + isConsumable + "\n");
ProductDetails skuDetails = productDetailsMap.get(productId);
StartQueryProducts(new String[]{productId}, productId, new Action() {
@Override
public void Invoke() {
ProductDetails skuDetails = productDetailsMap.get(productId);
System.out.print("-------2.查詢商品id -成功-返回商品信息 开始购

最低0.47元/天 解锁文章
483

被折叠的 条评论
为什么被折叠?



