I hope someone can help me. I need to notify user after he successfully purchased an item via in-app billing service. [...deleted confusing text...]
EDIT
TO be precise. From the demo Google project, I see that they use onRequestPurchaseResponse
to catch RESULT_OK
code. Inside of that method, I added the Toast and initiated the test purchase via android.test.purchased item. I got the Market's windows and message "Authorizing purchase". After the process completed, my Toast appeared.
@Override
public void onRequestPurchaseResponse(BillingService.RequestPurchase request,
Consts.ResponseCode responseCode) {
if (Consts.DEBUG) {
Log.d(TAG, request.mProductId + ": " + responseCode);
}
if (responseCode == Consts.ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.i(TAG, "purchase was successfully sent to server");
}
showToast("purchase was successfully sent to server");//<- MY TOAST!
logProductActivity(request.mProductId, "sending purchase request");
} else if (responseCode == Consts.ResponseCode.RESULT_USER_CANCELED) {
if (Consts.DEBUG) {
Log.i(TAG, "user canceled purchase");
}
logProductActivity(request.mProductId, "dismissed purchase dialog");
} else {
if (Consts.DEBUG) {
Log.i(TAG, "purchase failed");
}
logProductActivity(request.mProductId, "request purchase returned " + respons开发者_Go百科eCode);
}
}
My question is, is this the right place to place the message on successful purchase?
It's fine to put a message there, but that is not where the purchase gets completed. As you can see in the comments, it says that the purchase was successfully sent to the server. It does not mean that the purchase actually went through. This is called BEFORE your app is taken to Google Play to give the user the option to make the purchase. The user can still back out. If, however, the user presses BUY, the app restarts and the following code gets called.
@Override
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId, int quantity, long purchaseTime, String developerPayload) {
if (purchaseState == PurchaseState.PURCHASED) {
// PUT YOUR CONFIRMATION MESSAGE HERE
}
}
Just to confirm that this is the right place to do all after-successful-purchase actions.
精彩评论