我尝试使用guide provided by Google将InApp购买设置为捐赠,但是当我调用 Activity 时(无论成功还是失败,都应该返回),但会引发以下错误:

In-app billing error: Illegal state for operation (launchPurchaseFlow): IAB helper is not set up

使用以下方法调用该 Activity :
public boolean donation() {
        int success = 0;
        Intent intent = new Intent(Main.this, Donate.class);
        startActivityForResult(intent, success);
        if (success != 0) {
            return true;
        }
        else return false;
    }

而Donate类(我曾尝试与指南一起编码)如下所示:
import java.math.BigInteger;
import java.security.SecureRandom;
import maturaarbeit.nicola_pfister.marks.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;

public class Donate extends Activity {

    IabHelper mHelper;

    private static final String TAG = "Donate";
    private String payload;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String base64EncodedPublicKey = getKey();
        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {

            @Override
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    Log.d(TAG, "Problem setting up In-app Billing: " + result);
                }
            }
        });

        SharedPreferences prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);    //Add shared preferences
        payload = prefs.getString("devpayload", null);
        if (payload == null) {
            payload = getPayload();

            Editor editor = prefs.edit();
            editor.putString("devpayload", payload)
            .apply();
        }
        mHelper.launchPurchaseFlow(this, "donation_1chf", 1, mPurchaseFinishedListener, payload);
        finish();
    }

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {

        @Override
        public void onIabPurchaseFinished(IabResult result, Purchase info) {
            if (result.isFailure()) {
                Log.d(TAG, "Error purchasing: " +result);
                return;
            }
            else if (info.getDeveloperPayload().equals(payload)) {
                return;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        } else {
            Log.i(TAG, "onActivityResult handled by IABUtil.");
        }
    }

    private String getKey() {
        //Code for building public key
    }

    private String getPayload() {
      SecureRandom random = new SecureRandom();
      {
        return new BigInteger(130, random).toString(32);
      }
    }

    @Override
    protected void onDestroy() {
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
        super.onDestroy();
    }

}

如果还没有,则应该生成一个dev有效负载,该有效负载存储在共享首选项中。如果您需要任何其他信息,请随时询问。

谢谢你的帮助!

最佳答案

在调用onIabSetupFinishedListener之前,您必须等待launchPurchaseFlow返回-将这些行移动到onIabSetupFinishedListeneronIabSetupFinished方法中,以确保IabHelper的设置完整:

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG, "Problem setting up In-app Billing: " + result);
            }
            else { // Only launch the purchase flow if setup succeeded
                mHelper.launchPurchaseFlow(Donate.this, "donation_1chf", 1,
                    mPurchaseFinishedListener, payload);
            }
        }
    });

关于Android应用内结算错误: Illegal state for operation (launchPurchaseFlow): IAB helper is not set up,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16387775/

10-11 17:16