0

好的,所以我的 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 只是不更新了吗?为什么?

4

1 回答 1

2

确保您没有测试 queryInvetory 方法,因为它仅在您尝试购买真实产品而不是虚拟产品(android.test.purchased)时才有效,因为据我所知,谷歌不会保留虚拟产品的记录.

onCreate() 方法

在 oncreate 方法中定义所有变量,只需更改 queryInventory 和 IabPurchaseFinishListener 中的文本框启用-禁用。

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);

QueryInventoryFinishedListener

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 {                // you don't need to check for the failure then go if and other wise else.
Log.d(TAG, "Query inventory was successful.");
// does the user have the premium upgrade?
mIsPremium = inventory.hasPurchase(SKU_PREMIUM);

// update UI accordingly

if(mIsPremium){

     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.");
}
};  

OnIabPurchaseFinishedListener

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
    if (result.isFailure()) {
    Log.d(TAG, "Error purchasing: " + result);
    return;
    }
    if (purchase.getSku().equals(SKU_PREMIUM)) { // same as above you don't need to check else if condition again
    // give user access to premium content and update the UI

        mLevel6.setEnabled(true);
        mLevel7.setEnabled(true);
        mLevel8.setEnabled(true);
        mLevel9.setEnabled(true);
        mLevel10.setEnabled(true);
    }
    }
    };

让我知道它是否适合您。

于 2013-10-23T06:23:50.777 回答