Android应用内结算错误:非法操作状态(launchPurchaseFlow):未设置IAB帮助程序

时间:2013-05-05 18:36:51

标签: android in-app-purchase

我尝试使用guide provided by Google设置InApp购买捐款,但是当我调用该活动时(如果成功与否则应该返回)但是它会抛出此错误:

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

使用此方法调用活动:

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有效负载,如果还没有,则存储在共享的prefs中。如果您需要任何其他信息,请随时询问。

感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

在致电onIabSetupFinishedListener之前,您必须等待launchPurchaseFlow返回 - 将这些行移至onIabSetupFinishedListener的{​​{1}}方法,以确保onIabSetupFinished设置完成了:

IabHelper

答案 1 :(得分:2)

您应该使用真实设备进行应用结算,因为在使用模拟器时,某些参数为空并且会导致运行时错误。