onPurchaseStateChange not getting called

末鹿安然 提交于 2019-12-11 04:48:10

问题


The sample code for the in-app feature has:

onPurchaseStateChange(PurchaseState purchaseState, String itemId,
            int quantity, long purchaseTime, String developerPayload)

and it brings information on the in-app purchase that was made (id, how much etc.).

and here I can update my code but I am not getting any response in this callback method from Billing Service. What wrong in sample application. can anyone help ?

Thanks in Advance.


回答1:


I'm not sure what the problem is and why sometimes the @overrided onPurchaseStateChange in the requesting Activity isn't called but I can verify that onPurchaseStateChange in the BillingReceiver and BillingService are always called.

For some reason, sometimes, the callback just doesn't arrive all the way back to the calling Activity.

By the way, this issue seems to only happen when you purchase a managed product or a subscription. If you buy an unmanaged product, the callback will always arrive.

A solution might be (in the requesting purchase Activity - Dungeons Activity in the sample application):

@Override
        public void onPurchaseStateChange(PurchaseState purchaseState, String itemId, int quantity, long purchaseTime, String developerPayload) {

            if (purchaseState == PurchaseState.PURCHASED) {

                onPurchaseStateChange = true;

                // Do whatever you need to do after purchase here first.

            }
        }

        @Override
        public void onRequestPurchaseResponse(RequestPurchase request, ResponseCode responseCode) {


            if (responseCode == ResponseCode.RESULT_OK) {

                if (!onPurchaseStateChange) {
                    // If onPurchaseStateChange = false, the onPurchaseStateChange callback didn't arrive from the BillingService, so you can perform your after purchase actions here.
                }

            } else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {

            } else {

            }
        }

Solution #2 (seems to be the right solution): Google's sample code includes this method:

@Override
    protected void onStop() {
        super.onStop();

        ResponseHandler.unregister(YOUR_PURCHASE_OBSERVER);
    }

This causes the observer to be unregistered the second you open the Play store purchase activity. When you go back to your activity (whether after purchase or back button), the onStart is called and re-starts the purchaseObserver but not always with the purchase information - which causes the onPurchaseStateChange to never be called.

A solution will be to remove the unregister from onStop and move it to onDestroy. Another solution is to leave it there but start it with a queue that hold previously received details. See here: Android in-app-billing. When to unregister the ResponseHandler?



来源:https://stackoverflow.com/questions/12844132/onpurchasestatechange-not-getting-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!