[英]Android In-app billing error: Illegal state for operation (launchPurchaseFlow): IAB helper is not set up
I tried setting up InApp purchases for a donation with the guide provided by Google but when I call the activity (which should return if it was a success or not) but it throws this error: 我尝试使用Google提供的指南设置 InApp购买捐赠,但是当我调用该活动时(如果成功与否则应该返回)但是它会抛出此错误:
In-app billing error: Illegal state for operation (launchPurchaseFlow): IAB helper is not set up
The activity is called with this method: 使用此方法调用活动:
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;
}
And the Donate class (which I tried coding with the guide) looks like this: 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();
}
} }
It is supposed to generate a dev payload which is stored in shared prefs if there isn't already one. 它应该生成一个dev有效载荷,如果还没有,则存储在共享的prefs中。 If you need any additional information feel free to ask.
如果您需要任何其他信息,请随时询问。
Thanks for your help! 谢谢你的帮助!
You have to wait for onIabSetupFinishedListener
to return before calling launchPurchaseFlow
- move those lines into your onIabSetupFinishedListener
's onIabSetupFinished
method to ensure that the IabHelper
's setup is complete: 在调用
launchPurchaseFlow
之前,必须等待onIabSetupFinishedListener
返回 - 将这些行移动到onIabSetupFinishedListener
的onIabSetupFinished
方法中,以确保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);
}
}
});
您应该使用真实设备进行应用程序计费,因为在使用模拟器时,某些参数为空并且会导致运行时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.