简体   繁体   English

Libgdx和Google In-App-Purchase结果

[英]Libgdx and Google In-App-Purchase result

I followed these instructions to integrate both Libgdx and native android code using ActionResolver interface. 我按照这些说明使用ActionResolver接口集成了Libgdx和本机android代码。 I have no problem calling the Android method from the Libgdx part of my code. 我从我的代码的Libgdx部分调用Android方法没有问题。 But I am hitting a dead end when I am trying to intergrate Google IAP with Libgdx. 但是,当我尝试将Google IAP与Libgdx集成时,我正在走向死胡同。 According to TrivialDrive example, it uses mPurchaseFinishedListener (outside of calling method). 根据TrivialDrive示例,它使用mPurchaseFinishedListener(在调用方法之外)。
My question is: how do I pass this IAP resultcode back to Libgdx since the listener is outside the calling method? 我的问题是:如何将此IAP结果代码传递回Libgdx,因为侦听器在调用方法之外? Currently, purchase process went through, but the libgdx part of my code is not being "informed" of the purchase status/result. 目前,购买流程已经完成,但我的代码中的libgdx部分未被“通知”购买状态/结果。
This is my code: 这是我的代码:

Any help is much appreciated. 任何帮助深表感谢。

ActionResolver: ActionResolver:

public interface IActionResolver {

public int requestIabPurchase(int product);

}

MainActivity: 主要活动:

public class MainActivity extends AndroidApplication implements IActionResolver {

// Debug tag, for logging
static final String TAG = "greatgame";

// Does the user have the premium upgrade?
boolean mIsUpgraded = false;

// SKUs for our products: the cat, all, or pow
static final String SKU_UPGRADE = "android.test.purchased";

// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 10001;

// The helper object
IabHelper mHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useGL20 = false;

    initialize(new Catland(this), cfg);
}

void iAbStartup() {

    String base64EncodedPublicKey = "some key";

    // Create the helper, passing it our context and the public key to verify signatures with
    Log.d(TAG, "Creating IAB helper.");
    mHelper = new IabHelper(this, base64EncodedPublicKey);

    // enable debug logging (for a production application, you should set this to false).
    mHelper.enableDebugLogging(true);

    // Start setup. This is asynchronous and the specified listener
    // will be called once setup completes.
    Log.d(TAG, "Starting setup.");
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            Log.d(TAG, "Setup finished.");

            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                Log.d(TAG, "Problem setting up in-app billing: " + result);
                return;
            }

            // Have we been disposed of in the meantime? If so, quit.
            if (mHelper == null) {
                return;
            }

            // IAB is fully set up. Now, let's get an inventory of stuff we own.
            Log.d(TAG, "Setup successful. Querying inventory.");
            mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    });
}

// Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        Log.d(TAG, "Query inventory finished.");

        // Have we been disposed of in the meantime? If so, quit.
        if (mHelper == null) {
            return;
        }

        // Is it a failure?
        if (result.isFailure()) {
            Log.d(TAG, "Failed to query inventory: " + result);
            return;
        }

        Log.d(TAG, "Query inventory was successful.");

        // Do we have the SKU_UPGRADE upgrade?
        Purchase thisUpgrade = inventory.getPurchase(SKU_UPGRADE);
        mIsUpgraded = (thisUpgrade != null && verifyDeveloperPayload(thisUpgrade));
        Log.d(TAG, "User is " + (mIsUpgraded ? "Upgraded" : "Free"));
        Log.d(TAG, "Initial inventory query finished; enabling main UI.");
        runPurchaseFlow(submitProduct);
    }
};

// Run real purchase flow
public void runPurchaseFlow(int product) {
    Log.d(TAG, "runPurchaseFlow");

    /* TODO: for security, generate your payload here for verification. See the comments on
     *        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
     *        an empty string, but on a production app you should carefully generate this. */
    String payload = "";

    if (product == 1) 
        mHelper.launchPurchaseFlow(this, SKU_UPGRADE, RC_REQUEST, mPurchaseFinishedListener, payload);

}

// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        if (!verifyDeveloperPayload(purchase)) {
            Log.d(TAG, "Error purchasing. Authenticity verification failed.");
            return;
        }

        Log.d(TAG, "Purchase successful.");

        if (purchase.getSku().equals(SKU_CAT)) {
            // bought the upgrade!
            Log.d(TAG, "Purchase Upgrade. Congratulating user.");
            mIsUpgraded = true;
    // how do i pass this result to the libgdx?

        }
    }
};

/** Verifies the developer payload of a purchase. */
boolean verifyDeveloperPayload(Purchase p) {
    String payload = p.getDeveloperPayload();
    return true;
}

@Override
public int requestIabPurchase(int product) {

    iAbStartup();

    return 0; // how do i get the result from mPurchaseFinishedListener?
}

}

PurchaseScreen PurchaseScreen

result = greatgame.actionResolver.requestIabPurchase(1);

You won't be able to return the result from requestIabPurchase() - the only methods of doing so would block for a long time. 您将无法从requestIabPurchase()返回结果 - 这样做的唯一方法会阻塞很长时间。 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. 在我看来,最好的方法是创建一个LibGdx项目实现的自己的侦听器接口,并将其传递到您的请求接口。 For example: 例如:

In your libGdx project somewhere: 在你的libGdx项目的某个地方:

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

ActionResolver: ActionResolver:

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

In PurchaseScreen, implement PurchaseCallback: 在PurchaseScreen中,实现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): ...并传递任何实现PurchaseCallback的东西(我假设你的PurchaseScreen自己做):

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

Finally, hook it all up in MainActivity: 最后,将它全部挂在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? 请注意,您应该在mPurchaseFinishedListener return任何地方调用PurchaseCallback.setPurchaseResult(),而不仅仅是在// how do i pass this result to the libgdx?// 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. - 否则,你永远不会知道购买是否失败或者是否需要很长时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM