gpt4 book ai didi

Android 应用内计费错误 : Illegal state for operation (launchPurchaseFlow): IAB helper is not set up

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:45 26 4
gpt4 key购买 nike

我尝试使用 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();
}

它应该生成一个开发有效载荷,如果还没有的话,它会存储在共享首选项中。如果您需要任何其他信息,请随时询问。

感谢您的帮助!

最佳答案

在调用 launchPurchaseFlow 之前,您必须等待 onIabSetupFinishedListener 返回 - 将这些行移动到您的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com