1

这是实施应用内结算的代码的一部分。我有两个疑问。应用内结算实施:已确认购买

@Override 
    public void onPurchaseStateChange(PurchaseState purchaseState, String itemId, 
      int quantity, long purchaseTime, String developerPayload) { 
     if (Consts.DEBUG) { 
      Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState); 
     } 

     if (developerPayload == null) { 
      logProductActivity(itemId, purchaseState.toString()); 
     } else { 
      logProductActivity(itemId, purchaseState + "\n\t" + developerPayload); 
     } 

     if (purchaseState == PurchaseState.PURCHASED) { 
      mOwnedItems.add(itemId); 

      // At this point I have to put Premium changes 
     } 
    } 

我的问题:

  1. 点在哪里我说:“在这一点上我已经把保费的变化”,怎么我可以向你保证应用程序已经购买?

  2. 我了解到,一旦进行了购买,这可能需要几个小时才能生效。如何确保我的应用程序能够执行代码:“此时我必须进行高级更改”?

回答

0

如果达不到此块

if (purchaseState == PurchaseState.PURCHASED) { 
    ... 
} 

那么你知道购买的是成功的。根据我的经验,购买后的延迟时间不会超过几秒钟。但用户可以在确认之前关闭应用程序。

这就是为什么你应该实施restoreTransactions()以及。每次您的活动打开时,都会检查服务器是否已购买应用程序。

/** 
* A {@link PurchaseObserver} is used to get callbacks when Android Market sends 
* messages to this application so that we can update the UI. 
*/ 
private class MyPurchaseObserver extends PurchaseObserver { 
    public DownloaderPurchaseObserver(Handler handler) { 
     super(home, handler); 
    } 

    @Override 
    public void onBillingSupported(boolean supported) { 
     ... 
     yourBillingService.restoreTransactions(); 
     ... 
    } 

    @Override 
    public void onRestoreTransactionsResponse(RestoreTransactions request, 
      ResponseCode responseCode) { 
     if (responseCode == ResponseCode.RESULT_OK) { 
      // repeat your premium validation here 
     } else { 
      if (Consts.DEBUG) { 
       Log.e(TAG, "RestoreTransactions error: " + responseCode); 
      } 
     } 
    } 

} 
+0

NathanZ,感谢您的速度响应!我还有一个疑问。我的问题:当我打电话给MyBuyActivity.java时,我开始了相关的​​计费服务,但是当用户没有点击按钮时,计费服务永远不会启动。然后,如果我退还了价格,该应用将永远是Premium。我如何从主活动中调用结算服务?如果购买需要几个小时才能生效(尽管很少发生),也会出现同样的情况。对不起,我的英语不好。 – Pablo

+0

看到这篇文章关于退款:http://stackoverflow.com/questions/6817507/android-in-app-billing-cancel-payment – znat