Android IabHelper检查是否购买

时间:2014-08-11 20:23:57

标签: java android google-api billing

如果已经购买,我想开一个活动,否则我想打开购买活动。我进入了IabHelper班来做这件事,但我没有得到任何结果。

我哪里错了?

代码段:

private void Controlla_record_per_acquisto(){
     SQLiteDatabase db = new DatabaseHelper(getActivity()).getReadableDatabase();
    String controllo = "SELECT COUNT(_id) FROM table";
    Cursor c = db.rawQuery(controllo, null);

    while (c.moveToNext()){
        int numero_id = c.getInt(0);            

    if(numero_id >=1){  

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

            Intent intent = null;
            intent = new Intent(getActivity(), Ce.class); 
            startActivity(intent);
        }

       }
    };  

    }else   {       
        Intent intent = null;
        intent = new Intent(getActivity(), InAppBillingActivity.class); 
        startActivity(intent);
    }
    c.close();
    db.close();
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用此代码获取有关您库存的数据。

好文章:

http://developer.android.com/training/in-app-billing/purchase-iab-products.html

如何调用方法:

mHelper = new IabHelper(this, base64EncodedPublicKey);

mHelper.enableDebugLogging(true);

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {

if (!result.isSuccess()) {
return;
}

if (mHelper == null) return;
List<String> st = new ArrayList<String>();
st.add(ITEM_SKU);
mHelper.queryInventoryAsync(true,st,mGotInventoryListener);
}
});

如何查询商品的库存。

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {

if (mHelper == null) return;

if (result.isFailure()) {
return;
}

if(inventory.hasPurchase(ITEM_SKU)) {
Intent intent = null;
intent = new Intent(getActivity(), Ce.class); 
startActivity(intent);
}
}
};