android - 为什么 android-inapp-billing-v3 库需要两次尝试购买?

标签 android in-app-billing

我正在尝试使用 android-inapp-billing-v3 库在我的简单应用中实现应用内购买。我正在使用这个库(https://github.com/anjlab/android-inapp-billing-v3)

我遇到了一个奇怪的问题:您需要点击“购买”按钮两次才能购买该产品。让我逐步解释我的意思。

我们的产品没有被购买,我们点击“购买”按钮 并出现 Google Play 窗口 在这里我们点击“购买”并看到我们的交易成功完成 然后我们点击任意位置使 Google Play 窗口消失,而不是输入 onProductPurchased(..) nothnig hapens。 好的,我们再次点击“购买”按钮,并且没有任何其他 Google Play 窗口 onProductPUrchased 被访问。 我不知道这是库中还是我的实现中的某种错误。 (对不起我的英语)

这是我的代码:

public class BillingActivity extends Activity implements BillingProcessor.IBillingHandler {
    BillingProcessor bp;


    public final static String EXTRA_MESSAGE = "kepardvpn.client.MESSAGE";
    private String LOG_TAG = "BillingActivity";


    int mFlag;
    private String mResult;
    private String mEmail;
    private String mPassword;
    private Functions mFunctions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        mFlag = intent.getFlags();
        mEmail = intent.getStringExtra(MainActivity.EMAIL);
        mFunctions = Globals.mFunctions;


        mPassword = intent.getStringExtra(MainActivity.PASSWORD);

        bp = new BillingProcessor(this, null, this);

        mFunctions.AddLog(2, LOG_TAG, "onCreate");
    }

    // IBillingHandler implementation

    @Override
    public void onBillingInitialized() {
        /*
         * Called then BillingProcessor was initialized and its ready to purchase
         */
        mFunctions.AddLog(2, LOG_TAG, "onBillignInitilized ");
        try {
            switch (mFlag) {
                case 0: {
                    mFunctions.AddLog(2, LOG_TAG, "pay_per_month");
                    bp.purchase(this, "pay_per_month");
                }
                break;
                case 1: {
                    mFunctions.AddLog(2, LOG_TAG, "pay_per_month");
                    bp.purchase(this, "pay_per_year");
                }
                break;
            }
        } catch (Exception e) {
            mFunctions.AddLog(2, LOG_TAG, "onBillingInitialized exception:" + e.getMessage());
        }
    }

    @Override
    public void onProductPurchased(String productId, TransactionDetails details) {
          /*
           * Called then requested PRODUCT ID was successfully purchased
           */
        mFunctions.AddLog(2, LOG_TAG, "Product successfully puchased");
        try {
            if (bp.consumePurchase(productId))  // imediatly after puchase consume product
            {
                mFunctions.AddLog(2, LOG_TAG, "onProductPurchased " + "product have been consumed");
                String obj = StringToJsonObject("action", "checkPayment", "email", mEmail, "password", mPassword,
                        "d", "android", "responseData", java.net.URLEncoder.encode(details.purchaseInfo.responseData, "utf-8"),
                        "signature", java.net.URLEncoder.encode(details.purchaseInfo.signature, "utf-8"), "response", "text");

                mFunctions.setPaymentResponseData(details.purchaseInfo.responseData);
                mFunctions.setPaymentSignature(details.purchaseInfo.signature);
                mFunctions.ActivatePayAccountTask(obj);
                //mFunctions.updatePreferences();

            } else // product could not have been consumed
            {
                mFunctions.AddLog(2, LOG_TAG, "onProductPurchased " + "product could not have been consumed");
            }
        } catch (Exception e) {
            mFunctions.AddLog(2, LOG_TAG, "onProductPurchased exception:" + e.getMessage());

        }
        ;

    }

    @Override
    public void onBillingError(int errorCode, Throwable error) {

        switch (errorCode) {
            case Constants.BILLING_RESPONSE_RESULT_DEVELOPER_ERROR:

                mFunctions.AddLog(2, LOG_TAG, "onBillingError," + "Invalid arguments provided to the API." + "Error code:" + errorCode);
                break;
            case Constants.BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE:
                sendMessage(getString(R.string.billing_not_supported_message));
                break;
            case Constants.BILLING_RESPONSE_RESULT_USER_CANCELED:
                mFunctions.AddLog(2, LOG_TAG, "onBillingError," + "User canceled purchase");
                break;
        }
        mFunctions.AddLog(2, LOG_TAG, "Error code:" + errorCode);
    }


    @Override
    public void onDestroy() {
        if (bp != null)
            bp.release();
        mFunctions.AddLog(2, LOG_TAG, "onDestroy");
        super.onDestroy();
    }

    @Override
    public void onPurchaseHistoryRestored() {
        /*
         * Called then purchase history was restored and the list of all owned PRODUCT ID's
         * was loaded from Google Play
         */
        mFunctions.AddLog(2, LOG_TAG, "onPurchasedHistoryRestored");
    }

    void sendMessage(String message) {        
        mFunctions.ShowError(message);
    }

    public String StringToJsonObject(String... val) {
        JSONObject obj = new JSONObject();
        try {
            for (int i = 0; i < val.length - 1; i += 2) {
                obj.put(val[i], val[i + 1]);
            }
        } catch (Exception e) {
        }
        return obj.toString();
    }

}

最佳答案

你必须添加

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (!bp.handleActivityResult(requestCode, resultCode, data))
        super.onActivityResult(requestCode, resultCode, data);
}

关于android - 为什么 android-inapp-billing-v3 库需要两次尝试购买?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28340020/

相关文章:

android - 在 OpenGL ES2 模型旋转的旋转矩阵中切换设备轴

java - Android Studio 自定义声音基于主题通知 FCM

android - 谷歌地图实用程序中集群的背景渲染

android.test.purchased 项目的 Android 应用内结算错误

android - UnManaged 选项在 google play developer console 中不可用

android - Proguard 与应用内购买 "Duplicate zip entry"错误

java - 在日出/日落 Web 服务的 url 中使用 DatePicker 中的日期

android - 由 fill_parent TextView 重叠的圆角线性布局角

android - 应用内结算:无法设置 IAB Helper 异常(exception)

php - php服务器中的Android应用内计费签名验证