好的,所以我的 GUI 应该在购买完成后更新。我正在使用 android.test.purchase 并且 GUI 没有更新我应该担心还是不担心?
变量
IabHelper mHelper;
static final String TAG = "com.back.to.school.zone.readingLevelPicker";
// SKUs for our products: the premium upgrade (non-consumable)
static final String SKU_PREMIUM = "android.test.refunded";
// Does the user have the premium upgrade?
boolean mIsPremium = false;
// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 20;
onCreate 方法
String base64EncodedPublicKey = "<my key is in here>";
mHelper = new IabHelper(this, base64EncodedPublicKey);
//It is recommended to add more security than just pasting it in your source code;
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d(TAG, "Problem setting up In-app Billing: " + result);
}
// Hooray, IAB is fully set up!
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
购买所需物品的按钮
buyButton = (Button) findViewById(R.id.buyButtonS);
buyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mHelper.launchPurchaseFlow(readingLevelPicker.this, SKU_PREMIUM, 10001,
mPurchaseFinishedListener, "");
}
mLevel10之后需要启用的按钮之一和on create方法的结束
mLevel10.setEnabled(false);
mLevel10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(),
readingMode10.class); // Coming soon
startActivityForResult(myIntent, 0);
}
});
} //end of oncreate method
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
if (result.isFailure()) {
Log.d(TAG, "Failed to query inventory: " + result);
return;
}
else {
Log.d(TAG, "Query inventory was successful.");
// does the user have the premium upgrade?
mIsPremium = inventory.hasPurchase(SKU_PREMIUM);
// update UI accordingly
mLevel6 = (Button) findViewById(R.id.level6);
mLevel7 = (Button) findViewById(R.id.level7);
mLevel8 = (Button) findViewById(R.id.level8);
mLevel9 = (Button) findViewById(R.id.level9);
mLevel10 = (Button) findViewById(R.id.level10);
mLevel6.setEnabled(true);
mLevel7.setEnabled(true);
mLevel8.setEnabled(true);
mLevel9.setEnabled(true);
mLevel10.setEnabled(true);
Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
}
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
return;
}
else if (purchase.getSku().equals(SKU_PREMIUM)) {
// give user access to premium content and update the UI
mLevel6 = (Button) findViewById(R.id.level6);
mLevel7 = (Button) findViewById(R.id.level7);
mLevel8 = (Button) findViewById(R.id.level8);
mLevel9 = (Button) findViewById(R.id.level9);
mLevel10 = (Button) findViewById(R.id.level10);
mLevel6.setEnabled(true);
mLevel7.setEnabled(true);
mLevel8.setEnabled(true);
mLevel9.setEnabled(true);
mLevel10.setEnabled(true);
}
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + ","
+ data);
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
} else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
一些评论-据我了解,就计费方法而言,一切正常,因为使用 android.test.purchased 时,它说付款已完成。但是 UI 没有启用文本框,因此禁用了文本框,我试图在 mLevel7.setEnabled("true") 位置抛出一个断点,但它没有显示任何不寻常的东西没有出现故障 UI 只是不更新了吗?为什么?