应用内购买 Android,多次购买相同的商品而不消耗

In-App Purchase Android,Buy same item over and over without consumption

很好 day.I 正在对 android.The 实施应用内购买,只有 coins.And 可以购买创意硬币的物品 over.But 因为我们知道 google play 应用程序内购买保持购买时的物品,你只能在 consumption.So 之后购买它什么是这种情况的最佳做法?就像我想让用户购买相同的硬币包over.What 是它的代码吗? 这是我用于应用内购买流程的代码。

 private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceDisconnected(ComponentName name) {
        mService = null;
    }

    @Override
    public void onServiceConnected(ComponentName name,
                                   IBinder service) {
        mService = IInAppBillingService.Stub.asInterface(service);

        try {
            Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
            Log.d("Fasfsafasfasfa", "onServiceConnected: "+ownedItems);

            int response = ownedItems.getInt("RESPONSE_CODE");
            Log.d("Fasfsafasfasfa", "onServiceConnected: "+response);

            if (response == 0) {
                ArrayList<String> ownedSkus =
                        ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
                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("Fasfsafasfasfa", "onServiceConnected: "+ownedSkus.size());
                Log.d("Fasfsafasfasfa", "onServiceConnected: "+purchaseDataList.size());
                Log.d("Fasfsafasfasfa", "onServiceConnected: "+signatureList.size());
                Log.d("Fasfsafasfasfa", "onServiceConnected: "+continuationToken);

                for (int i = 0; i < purchaseDataList.size(); ++i) {
                    String purchaseData = purchaseDataList.get(i);
                    String signature = signatureList.get(i);
                    String sku = ownedSkus.get(i);

                    // do something with this purchase information
                    // e.g. display the updated list of products owned by user
                    Log.d("Fasfsafasfasfa", "onServiceConnected: " + "purchased items are" + sku + " " + signature + " " + purchaseData);
                }

                // if continuationToken != null, call getPurchases again
                // and pass in the token to retrieve more items
            }


            Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
                    "small_pack", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
            PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
            startIntentSenderForResult(pendingIntent.getIntentSender(),
                    1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
                    Integer.valueOf(0));
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    }
};

我注意到了这个问题,因为在第一次购买时一切正常,每当我再次尝试购买相同的 sku 命名商品时,我都会遇到这样的致命异常

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.IntentSender android.app.PendingIntent.getIntentSender()' on a null object reference
                                                 at ink.va.activities.BuyCoins.onServiceConnected(BuyCoins.java:103)

我认为这意味着我必须先消费购买,然后再让用户购买..我说的对吗?请问谁对这个问题有建议?

消费您的物品以使其可以再次购买:

int response = mService.consumePurchase(3, getPackageName(), token);

警告:不要在主线程调用consumePurchase方法。调用此方法会触发网络请求,这可能会阻塞您的主线程。相反,创建一个单独的线程并从该线程内部调用 consumePurchase 方法。

同一个用户可以多次购买易耗品。

非消耗品只能购买一次。