Android应用内结算,非消耗性物品 [英] Android in-app billing, non consumable items

查看:99
本文介绍了Android应用内结算,非消耗性物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施应用内结算,以便用户可以购买对高级内容的访问权限.这是典型的非消耗性物品. (假设高级内容是问题应用中的额外问题或类别)

I'm implementing in-app billing where the user shall be able to buy access to premium content. This is typical non-consumable items. (Let's say the premium content is extra questions or categories in an question-app)

我已使用教程来创建第一个版本.问题是我将如何实施非消耗性部分.

I have used this tutorial to create the first version. The problem is how I shall implement the non-consumable part..

我怎么知道用户已经购买了高级内容?我想到的一种解决方案是在我的问题表中添加一个最初为"0"的列.当用户购买高级访问权限时,此列设置为"1". 这是要走的路吗?

How do I know that the user has bought the premium content? One solution I'm think of is to have a column in my question-table that is initially "0". When the user buy premium access, the this column is set to "1". Is this a way to go?

我在代码中的哪个位置从计费API收到消息,内容已被购买? (如果是..)

Where in my code do I get the message from the billing API that the content is already bought? (If it is..)

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

My code.. (From tutorial, "buy the possibility to click a button")

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

}

我阅读了此问题,表明我不应该呼叫

I read this question indicating that I not should call the

mHelper.consumeAsync(purchase,mConsumeFinishedListener);

mHelper.consumeAsync(purchase, mConsumeFinishedListener);

但是将其删除还可以吗?如果用户尝试第二次购买,该如何处理?

But is just removing it ok? Where should I handle the case that the user tries a second buy?

推荐答案

如果您不希望消费,那么就不要使用ConsumerAsync.

If you don't want consume, then don't use 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的关键部分是:

You can use the sharedpref to store the purchase info, and also check every onCreate of the activity and update the sharedpref accordingly. The key part on how to check if a SKU is purchased is:

     boolean hasPurchased_ITEM_SKU_PURCHASE_1 = inv.hasPurchase(ITEM_SKU_PURCHASE_1);

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

Do your query sync in your IAP setup and update your UI accordingly.

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

这篇关于Android应用内结算,非消耗性物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆