Ho lavorato con un campione di acquisto in-ap. La mia applicazione ha 2 pulsanti, il primo pulsante era disabilitato di default, fare clic sul secondo pulsante per acquistare e abilitare il primo pulsante. Il pulsante di acquisto funziona correttamente ma dopo l'acquisto, ho controllato l'inventario ma è sempre restituito null, significa che non ho ancora acquistato.inventory.getPurchase() restituisce sempre null sebbene sia stato acquistato già
Ecco il codice sorgente:
Setup:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buyButton = (Button) findViewById(R.id.buyButton);
clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setEnabled(false);
String base64EncodedPublicKey = "key";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " + result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
// Query to detect user was buy this item or not
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
});
buyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (clickButton.isEnabled()) {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
} else {
buyClick(v);
}
}
});
}
Il buy elemento clic:
public void buyClick(View view) {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "");
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
// Handle error
return;
} else if (purchase.getSku().equals(ITEM_SKU)) {
// consumeItem();
// buyButton.setEnabled(false);
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
}
};
L'ascoltatore:
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
Toast.makeText(getApplicationContext(),
"Query Inventory Error!", Toast.LENGTH_SHORT).show();
// Handle failure
} else {
Toast.makeText(getApplicationContext(),
"Query Inventory Success!", Toast.LENGTH_SHORT).show();
// mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
// mConsumeFinishedListener);
// if (inventory.hasPurchase(ITEM_SKU)) {
// clickButton.setEnabled(true);
// }
Purchase item = inventory.getPurchase(ITEM_SKU);
if (item != null) {
clickButton.setEnabled(true);
} else {
Toast.makeText(getApplicationContext(),
"This item was not buy yet!", Toast.LENGTH_SHORT)
.show();
}
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (result.isSuccess()) {
Toast.makeText(getApplicationContext(), "Consume done!",
Toast.LENGTH_SHORT).show();
clickButton.setEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "Consume Error!",
Toast.LENGTH_SHORT).show();
// handle error
}
}
};
Quando primo clic su Acquista pulsante, mostra il pagamento di alog, dopo aver eseguito il pagamento con successo, il clickButton è stato abilitato.
Ma quando faccio clic su buyButton alla seconda volta, si passa all'inventario ma lo inventory.getPurchase (ITEM_SKU) restituisce sempre null.
Qualche idea?
Grazie!
Secondo la tua domanda, penso che stai cercando di acquistare un prodotto per una volta, ho ragione? Se sì, non è necessario chiamare consumeFinish handler per l'acquisto una tantum. – Maulik
basta commentare il tuo codice da onIabFinishListner "mHelper.consumeAsync (acquisto, mConsumeFinishedListener);" per ulteriori informazioni: http: //stackoverflow.com/questions/19195864/android-inapp-billing-error-response-7item-already-owned/19218084#19218084 e prova ad acquistare nuovamente e fammi sapere che sarà risolto o no . – Maulik
@Maulik Cerco di consentire all'utente di acquistare l'articolo solo una volta. Se l'utente rimuove l'applicazione e si installa nuovamente, controllerò se l'utente lo ha già acquistato, quindi l'utente non ha bisogno di acquistarlo di nuovo. –