如何检查设备是否支持android中的应用程序内计费v3

时间:2017-08-28 09:36:26

标签: android in-app-purchase in-app-billing

我想制作应用内购买应用。我有一个代码。我有谷歌游戏控制台所有设置。但是id不起作用。它给了我这个错误: “IabResult:检查计费v3支持时出错。(回复:3:结算不可用)” 所以我想检查我的设备是否支持应用程序内计费v3。这是我的代码:

package com.khabuko.dtv;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.khabuko.dtv.util.IabHelper;
import com.khabuko.dtv.util.IabResult;
import com.khabuko.dtv.util.Inventory;
import com.khabuko.dtv.util.Purchase;

public class InAppBillingActivity extends AppCompatActivity {

private static final String TAG =
        "InAppBilling";
IabHelper mHelper;
static final String ITEM_SKU = "com.khabuko.item";

private Button clickButton;
private Button buyButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_app_billing);

    buyButton = (Button)findViewById(R.id.buyButton);
    buyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buyClick(v);
        }
    });
    clickButton = (Button)findViewById(R.id.clickButton);
    clickButton.setEnabled(false);
    String base64EncodedPublicKey =
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn+naZOOOHNVdP0zQMkeX5mFwBb9BU78cg4oqAxtY8gaMMuEHAaHBwU7yXW6zVh9uTof23SO4GzxTJcIPXCeTyMFmcoFkNvPtqZSFqYcqA5M8Ief0tzpXr81aLEe0lxSy/t3VTN29UA+AaXjt3bvsDXQQPUQXr1HEONfix6TsudI6SCILftZTMIfRZYfOU+0OJdi7J8uDkU2TBjz40UhGc4SWmCoANXVM5yjZ8w4jspXXmej7pP52NMb5nAlK5NrAgIEjHhSDrf8Sl0DhNwLTJM7e2yOWOPt/MvdDsQo1ensm2sSH0jwn1K04RVC8AIGPqtzxLZgP+Ysby0HKpMSU3wIDAQAB";

    mHelper = new IabHelper(this, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                                   public void onIabSetupFinished(IabResult result) {
                                       Log.d(TAG,"45-46");
                                       if (!result.isSuccess()) {
                                           Log.d(TAG, "In-app Billing setup failed: " +
                                                   result);
                                       } else {
                                           Log.d(TAG, "In-app Billing is set up OK");
                                       }
                                   }
                               });
}

public void buttonClicked (View view)
{
    clickButton.setEnabled(false);
    buyButton.setEnabled(true);
}

public void buyClick(View view) {
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
            mPurchaseFinishedListener, "mypurchasetoken");
}

@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.");
    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result,
                                      Purchase purchase)
    {
        if (result.isFailure()) {
            // Handle error
            return;
        }
        else if (purchase.getSku().equals(ITEM_SKU)) {
            consumeItem();
            buyButton.setEnabled(false);
        }

    }
};
public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
        = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
                                         Inventory inventory) {

        if (result.isFailure()) {
            // Handle failure
        } else {
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                    mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
        new IabHelper.OnConsumeFinishedListener() {
            public void onConsumeFinished(Purchase purchase,
                                          IabResult result) {

                if (result.isSuccess()) {
                    clickButton.setEnabled(true);
                } else {
                    // handle error
                }
            }
        };

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


}

1 个答案:

答案 0 :(得分:0)

IabHelper.OnIabSetupFinishedListener()中检查应用内结算可用性。

但是你必须在创建新SKU后等一段时间。 Google控制台中的任何修改都不会立即发生。