我想在我的应用程序中添加一个简单的非消费产品。按照此
billing_library_overview和此billing_testing指令中,我决定从保留的产品ID android.test.purchased
开始。我收到“付款成功”消息,但是当我在queryPurchases()
方法中调用android.test.purchased
方法时,它似乎只有一次返回了onResume()
的购买清单。此后每次调用queryPurchases()
时,我都会拥有purchasesResult.getPurchasesList()等于null。
这是代码的重要组成部分:
public class MySettings extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener, PurchasesUpdatedListener {
private BillingClient mBillingClient;
private Boolean bought = false;
private String product_id = "android.test.purchased";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mBillingClient = BillingClient.newBuilder(this).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
List<String> skuList = new ArrayList<>();
skuList.add("android.test.purchased"); //I know it is not necessary
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) {
}
}
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
}
});
checkPurchase();
createPreferenceScreen();
}
@Override
protected void onResume(){
checkPurchase();
createPreferenceScreen();
super.onResume();
}
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK) {
}
else {
}
}
private void checkPurchase(){
Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);
if (purchasesResult.getPurchasesList() != null) {
for (Purchase purchase : purchasesResult.getPurchasesList()){
if (product_id.equals(purchase.getSku())) {
bought = true;
}
}
}
}
...
}
在我的createPreferenceScreen()
方法中,我检查Boolean bought
是对还是错。如何在不使用自己的后端服务器的情况下恢复已购买的android.test.purchased
?
编辑:我听不懂,但是当我将应用内结算逻辑移至主要活动时,它开始起作用。