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. 据我了解,一旦购买,可能需要几个小时才能生效。如何确保我的应用程序将执行正确的代码:“此时我必须进行高级更改”?

4

1 回答 1

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);
            }
        }
    }

}
于 2012-12-04T19:03:08.837 回答