Libgdx and Google In-App-Purchase result

大兔子大兔子 提交于 2019-12-03 16:11:46

You won't be able to return the result from requestIabPurchase() - the only methods of doing so would block for a long time. The best way, in my opinion, would be to create a listener interface of your own that your LibGdx project implements, and pass that into your request interface. For example:

In your libGdx project somewhere:

interface PurchaseCallback {
    public int setPurchaseResult(int result);
}

ActionResolver:

public interface IActionResolver {
    public int requestIabPurchase(int product, PurchaseCallback callback);
}

In PurchaseScreen, implement PurchaseCallback:

@override
public int setPurchaseResult(int result) {
    // Yay! I have a result from a purchase! Maybe you want a boolean instead of an int? I don't know. Maybe an int (for the product code) and a boolean.
}

...and pass whatever is implementing PurchaseCallback (I'm assuming your PurchaseScreen does itself):

result = greatgame.actionResolver.requestIabPurchase(1, this);

Finally, hook it all up in MainActivity:

PurchaseCallback mCallback = null;

mPurchaseFinishedListener = ... etc. etc.
.
.
.
   if (mCallback != null) {
       mCallback.setPurchaseResult(0);
   }
.
.
.

@Override
public int requestIabPurchase(int product, PurchaseCallback callback) {
    mCallback = callback;  // save this for later

    iAbStartup();

    return 0;
}

Note that you should call PurchaseCallback.setPurchaseResult() everywhere that mPurchaseFinishedListener has return, not only at the line // how do i pass this result to the libgdx? - otherwise, you will never know if a purchase failed or is just taking a really long time.

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