我只能购买一个项目(" Productitem1"(。如果我购买了此商品,我将无法再次购买。但是我需要它购买几次。在我的Google Play控制台中,我只能在"托管内应用程序内产品"one_answers" Subs"之间进行选择。我已经将其设置为"托管内应用产品"。
@Override
protected void onActivityResult(int request, int response, Intent data) {
if (request == 42) {
int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
String dataSignature = data.getStringExtra("INAPP_DATE_SIGNATURE");
if (response == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String productId = jo.getString("productId");
Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Log.e(getClass().getSimpleName(), "JSONException", e);
}
}
}
}
btnBuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = "productitem1";
try {
Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), name, "inapp", "");
if(buyIntentBundle.getInt("RESPONSE_CODE")==0) {
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
startIntentSenderForResult(
pendingIntent.getIntentSender(), 42, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
}
} catch (Exception e) {
Log.e(Start.this.getClass().getSimpleName(),"Exception:",e);
}
}
});
在您可以使用同一SKU购买另一个项目之前,您需要使用APP In-App In-App账单API(例如IabHelper.consumeAsync()
mHelper.consumeAsync(inventory.getPurchase(SKU_ITEM), mConsumeFinishedListener);
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess()) {
// provision the in-app purchase to the user
// (for example, credit 50 gold coins to player's character)
} else {
// handle error
}
}
};
更多信息在这里:https://developer.android.com/training/in-app-billing/purchase-ab-products.html#consume
IabHelper.OnConsumeFinishedListener onConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
@Override
public void onConsumeFinished(Purchase purchase, IabResult result) {
try {
Log.d("Tag", "this is onConsumeFinished - "+result.toString()+" and purchased"+purchase.toString());
mHelper.consumeAsync(purchase,onConsumeFinishedListener);
} catch (IabHelper.IabAsyncInProgressException e) {
Log.d("Tag", "this is onConsumeFinished Error - "+e.toString());
e.printStackTrace();
}
}
};