我在网上关注了一个教程。我把所有这些代码都放在了我的onCreate
之前IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase,
IabResult result) {
if (result.isSuccess()) {
btnPurchase.setEnabled(false);
btnStats.setBackgroundResource(R.drawable.purchasepressed);
test.setText("IT WORKS!!!");
} else {
// handle error
}
}
};
public void consumeItem() throws IabHelper.IabAsyncInProgressException {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
try {
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) throws IabHelper.IabAsyncInProgressException {
if (result.isFailure()) {
// Handle failure
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), mConsumeFinishedListener);
}
}
};
public void buyClick(View view) throws IabHelper.IabAsyncInProgressException {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "mypurchasetoken");
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase) throws IabHelper.IabAsyncInProgressException {
if (result.isFailure()) {
// Handle error
return;
} else if (purchase.getSku().equals(ITEM_SKU)) {
btnPurchase.setEnabled(false);
btnPurchase.setBackgroundResource(R.drawable.purchasepressed);
test.setText("IT WORKS!!!");
}
}
};
我的印象是购买后.getSKU()。equals(ITEM_SKU) 如果购买成功,你可以放置任何应该执行的代码。
正如您在此处所见,如果购买成功,我的购买按钮应被禁用并更改其背景图像。我还添加了一个测试文本标签,以查看错误是否在我的代码中的其他位置。
当我在手机上运行代码时,我会看到购买对话框并取得成功,但按钮和文字不会改变。
请帮忙。