这是实施应用内结算的代码的一部分。我有两个疑问。
@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
}
}
我的问题:
在我说“此时我必须进行高级更改”时,我如何向您保证已购买该应用程序?
据我所知,一旦购买,可能需要几个小时才能生效。我如何确保我的应用程序将执行恰当的代码:“此时我必须进行高级更改”?
答案 0 :(得分: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);
}
}
}
}