0

美好的一天。我正在 android 中实现应用内购买。唯一可以购买的物品是硬币。按照想法,硬币可以一遍又一遍地购买。但正如我们所知,谷歌播放应用内购买会保持物品为已购买,你只能在消费后购买。那么这种情况的最佳做法是什么?就像我想让用户一遍又一遍地购买相同的硬币包。它的代码是什么?这是我用于应用内购买流程的代码。

 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$1.onServiceConnected(BuyCoins.java:103)

我可以认为这意味着我必须首先消费购买,然后才能再次让用户购买它。我是对的吗?无论如何谁有这个问题的建议?

4

1 回答 1

1

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

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

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

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

非消耗品只能购买一次。

于 2016-09-14T13:16:26.450 回答