恢复Android中的app购买项目

时间:2014-04-12 09:27:11

标签: android in-app-purchase google-play in-app-billing

我试图让用户能够从我的应用程序中恢复他的inapp购买商品。

我使用以下代码:

     ServiceConnection mServiceConn = new ServiceConnection() 
    {




        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
             mService = IInAppBillingService.Stub.asInterface(service);

                Log.d("TEST", "mService ready to go!");
                checkownedItems();    

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
               mService = null;  

        }
    };




    public void checkownedItems()
    {
        try
        {
             String base64EncodedPublicKey = "MY_KEY";
        //    Log.d("getPackageName ", "Items Owned: " + getPackageName());
            Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);

            if(ownedItems.getInt("RESPONSE_CODE") == 0)
            {


                ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");

                //ownedSkus.toArray();
                ownedSkus.add("test");

                for (int n=0;n<ownedSkus.size();n++)
                   {
                            Log.d("Items: ", "Items: " + ownedSkus.get(n));

                   }  

                ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
                ArrayList<String> signatureList = ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE_LIST");
                String continuationToken = ownedItems.getString("INAPP_CONTINUATION_TOKEN");
                //Log.d("Items Owned: ", "Items Owned: " + ownedSkus.size()); toast
                Log.d("Data List: ", "Data List: " + purchaseDataList.size());
                Log.d("signature List: ", "signature List: " + signatureList);
                Log.d("continuation Token: ", "continuation Token: " + continuationToken);
                if(purchaseDataList.size() > 0)
                {
                    //Item(s) owned
                     Log.d("purchaseDataList: ", "purchaseDataList: " + purchaseDataList.size());
                    for(int i=0; i<purchaseDataList.size(); ++i)
                    {






                        String sku = ownedSkus.get(i);


 /*         String xx =          ownedItems.getString("INAPP_CONTINUATION_TOKEN"); 

                            Log.d(TAG, "INAPP_CONTINUATION_TOKEN: " + xx);
                                */

                                        Log.d(TAG, "Item_SKU: " + "consume");
                                        String bookpdf = "http://www.mybook.com/"+sku+ ".pdf";


                                    String Bookvalue=   sku.substring(sku.lastIndexOf(".")+1);

                                        downloadAndEncrypt(bookpdf,Bookvalue + ".pdf");
                                        Log.d(TAG, "Item_SKU-Download: "
                                                + "Download Complete");
                                        Log.d("OWNED_ITEMS_FINAL_SIZE","OWNED_ITEMS_FINAL_SIZE: " + ownedItems.size() );
                                        Log.d("OWNED_ITEMS_FINAL_LIST","OWNED_ITEMS_FINAL_LIST: " + ownedItems.keySet()); 
                                        Log.d("ownedSkus1","ownedSkus1: " + ownedSkus.size() );
                                        Log.d("ownedSkus2","ownedSkus2: " + ownedSkus.toString()); 
                                        Log.d("ownedSkus3","ownedSkus3: " + ownedSkus); 

                                        //Log.d("testtt","testtt " + "INAPP_PURCHASE_ITEM_LIST".length());



                                    /*  String value = ownedItems.getString("INAPP_PURCHASE_ITEM_LIST");
                                        Log.d("my_test","my_test: " + value); */

                    }
                }
                else
                {
                    //Item(s) not owned


                    mHelper = new IabHelper(this, base64EncodedPublicKey);

                    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
                    {


                        @Override
                        public void onIabSetupFinished(IabResult result) {
                            // TODO Auto-generated method stub
                             if(!result.isSuccess())
                                {
                                    Log.d("TEST", "In-app Billing setup failed: " + result);
                                }
                                else
                                {
                                    Log.d("TEST", "In-app Billing is set up OK");
                                }

                        }
                    });
                }
            }
            else
            {
                //Error checking owned items
            }
        }
        catch(RemoteException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();

        if(mServiceConn != null)
        {
            //unbindService(mServiceConn);
        }
    }

请注意,我使用了应用程序的正确base64EncodedPublicKey键。

现在问题是上面的代码只返回从我的应用程序购买的第一个inapp项。我尝试使用那段代码尝试了很多东西,并且在尝试记录输出结果时它总是只返回第一项。

请告知。

谢谢。

2 个答案:

答案 0 :(得分:0)

以下是您要查找的代码:

private void queryUserPurcahses()
{
    mHelper.queryInventoryAsync(mGotInventoryListener);
}

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

      if (result.isFailure()) {
        // handle error here
      }
      else {
            boolean is_purchased_1 = inventory.hasPurchase(product_1_ID);  
            boolean is_purchased_2 = inventory.hasPurchase(product_2_ID);  

      }
   }
};

答案 1 :(得分:0)

我明白了。我在代码中消费了所购买的商品。我做了什么我只是评论了consumeitem()函数,当我发布我的apk(它必须发布以返回所有购买的项目时,不知道为什么!)它就像一个魅力并恢复了我的所有项目我在评论了consumeitem()之后购买了。

谢谢大家。