Android应用内结算,非消耗品

时间:2014-04-29 16:36:19

标签: android in-app-billing

我实施应用内结算,用户应能够购买优质内容的访问权限。这是典型的非消耗品。 (让我们说优质内容是问题应用中的额外问题或类别)

我使用this教程创建了第一个版本。问题是我将如何实施非消耗性部件..

我如何知道用户购买了优质内容?我想到的一个解决方案是在我的问题表中有一个列,最初是" 0"。当用户购买高级访问权限时,此列设置为" 1"。 这是一种方法吗?

在我的代码中,我从结算API获取内容已经购买的消息? (如果是..)

我的代码..(来自教程,"购买单击按钮的可能性")

public class BuyExtras extends Activity {

private static final String TAG = "inAppBilling";
static final String ITEM_SKU = "android.test.purchased";
IabHelper mHelper;
private Button clickButton;
private Button buyButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate CALLED");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buy_extras);


    buyButton = (Button)findViewById(R.id.buyButton);
    clickButton = (Button)findViewById(R.id.clickButton);   
    clickButton.setEnabled(false);



    String base64EncodedPublicKey = 
            "<myKey>";

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


}

public void buyClick(View view) {
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001, mPurchaseFinishedListener, "mypurchasetoken");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {     
        super.onActivityResult(requestCode, resultCode, data);
    }
}


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

    }
};

public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if (result.isFailure()) {
            // Handle failure
        } else {
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), mConsumeFinishedListener);
        }
    }
};


IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    public void onConsumeFinished(Purchase purchase, IabResult result) {

        if (result.isSuccess()) {         
            clickButton.setEnabled(true);
        } else {
            // handle error
        }
    }
};

public void buttonClicked (View view)
{
    clickButton.setEnabled(false);
    buyButton.setEnabled(true);
}


@Override
public void onDestroy() {
    super.onDestroy();
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
}

}

我读了这个question,表示我不应该打电话给

  

mHelper.consumeAsync(purchase,mConsumeFinishedListener);

但是只是把它删除好吗?我应该在哪里处理用户尝试第二次购买的情况?

1 个答案:

答案 0 :(得分:2)

如果您不想消费,请不要使用consumeAsync。

 @Override
public void onQueryInventoryFinished(IabResult result, Inventory inv)
{
    if (result.isFailure())
    {
        Log.e(TAG, "In-app Billing query failed: " + result);
        return;
    } else
    {
        boolean hasPurchased_ITEM_SKU_PURCHASE_1 = inv.hasPurchase(ITEM_SKU_PURCHASE_1);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(KEY_PREF_PURCHASE_1_AVAILABLE, !hasPurchased_ITEM_SKU_PURCHASE_1);
        editor.commit();

        // You can update your UI here, ie. Buy buttons.
    }
}

您可以使用sharedpref存储购买信息,还可以检查活动的每个onCreate并相应地更新sharedpref。 关于如何检查是否购买SKU的关键部分是:

     boolean hasPurchased_ITEM_SKU_PURCHASE_1 = inv.hasPurchase(ITEM_SKU_PURCHASE_1);

在IAP设置中同步查询并相应地更新您的用户界面。

 mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
    public void onIabSetupFinished(IabResult result) 
    {
        if (!result.isSuccess()) {
            Log.d(TAG, "In-app Billing setup failed: " + 
                    result);
        } else {             
              mHelper.queryInventoryAsync(mReceivedInventoryListener);
            Log.d(TAG, "In-app Billing is set up OK");
        }
    }
 });