【问题标题】:In-App Purchase Android,Buy same item over and over without consumption应用内购买Android,一遍又一遍地购买相同的物品而无需消费
【发布时间】:2017-01-22 07:19:13
【问题描述】:

美好的一天。我正在 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)

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

【问题讨论】:

  • 当您再次尝试购买相同的产品时,LOGCAT 是什么登录??
  • 如果已经购买,您必须在再次购买之前消费 inapp产品。
  • 异常是logcat。除了消费没有别的办法???
  • 我的意思是每当我再次尝试购买时,我都会遇到异常......

标签: android in-app-purchase google-play-services purchase-order android-inapp-purchase


【解决方案1】:

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

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

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

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

非消耗品只能购买一次。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 2015-12-09
相关资源
最近更新 更多