gpt4 book ai didi

android - 应用内结算 onPurchasesUpdated 未调用

转载 作者:行者123 更新时间:2023-11-29 02:23:09 26 4
gpt4 key购买 nike

我正在测试应用程序内结算的应用程序 我在 google play 控制台中设置了我的帐户,一切正常,购买成功,但最后一个方法 onPurchasesUpdated 我不知道它是否调用或不,但我没有得到任何 toast ,所以它可能不会调用我的代码有问题吗?该应用程序告诉我购买成功我如何在购买成功时编写代码?

public class MainActivity extends AppCompatActivity implements PurchasesUpdatedListener {

BillingClient mBillingClient;

private Button mBtnBuy;

private String product = "product_test_4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mBtnBuy = findViewById(R.id.mBtn_buy);

mBtnBuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {



}


}).build();

mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(int responseCode) {
startPurchase();
}

@Override
public void onBillingServiceDisconnected() {

}
});
}
});


}

public void startPurchase() {

List<String> skuList = new ArrayList<>();
skuList.add(product);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
if (responseCode == BillingClient.BillingResponse.OK
&& skuDetailsList != null) {
for (SkuDetails skuDetails : skuDetailsList) {
String sku = skuDetails.getSku();
String price = skuDetails.getPrice();
if (product.equals(sku)) {

}
}
}
}
// Process the result.
});

BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSku(product)
.setType(BillingClient.SkuType.INAPP) // SkuType.SUB for subscription
.build();
int responseCode = mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
}


@Override
public void onPurchasesUpdated(@BillingClient.BillingResponse int responseCode, List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK
&& purchases != null) {
Toast.makeText(this, "purchase success", Toast.LENGTH_SHORT).show();
Log.d("tag","purchase success");
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
Toast.makeText(this, "purchase cancelled", Toast.LENGTH_SHORT).show();
Log.d("tag","purchase cancelled");
} else {
// Handle any other error codes.
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
Log.d("tag","purchase error");
}
}

}

最佳答案

您正在 mBtnBuy onClick 中设置您的 PurchasesUpdatedListener

mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {



}


}).build();

所以该方法将在购买时调用而不是最后一个 onPurchasesUpdated

你可以做到 mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener().build();像这样然后最后onPurchasesUpdated将被调用

关于android - 应用内结算 onPurchasesUpdated 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53943979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com