IAB helper is not set up. Can't perform operation: queryInventory

半世苍凉 提交于 2019-12-03 00:37:33
Georgi Koemdzhiev

Finally, I got the whole thing to work and not got this exception "IAB helper is not set up. Can't perform operation: queryInventory" or one more that I was getting "Can't start async operation (consume) because another async operation(consume) is in progress".

The first thing that you have to do is to add 2 methods in the IabHelper.java class:

 public boolean isAsyncInProgress(){
    return mAsyncInProgress;
}
public boolean isSetupDone (){
    return mSetupDone;
}

and than on your main activity:

public class MainActivity extends AppCompatActivity {
private   IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    @Override
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if (result.isFailure()) {
            // handle error here
            Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();
        }
        else{
            // does the user have the premium upgrade?
            mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
            if(!mIsRemoveAdds) {
                Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
                mAdView = (AdView) findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }else{
                mAdView.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
            }

        }
    }
};
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) {
            // consume the gas and update the UI
            mIsRemoveAdds = true;
            mAdView.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show();
        }
    }
};

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
String publicKey = s1+s2+s3+s4+s5;

    mHelper = new IabHelper(this,publicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                //error
                Log.d(TAG, "Proglem setting up in-app Billing: " + result);
            }
            if (result.isSuccess()) {
                //Horay, IAB is fully set up!
                Log.d(TAG, "Horay, IAB is fully set up!");
                //queryPurchasedItems;
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        }
    });

And now, as it is recommended in Google's guide for the API we have to check what items the user has bought in on start or on resume methods. Here we are going to need the two methods that we added in IabHelper.java class.

   private void queryPurchasedItems() {
    //check if user has bought "remove adds"
    if(mHelper.isSetupDone() && !mHelper.isAsyncInProgress()) {
        mHelper.queryInventoryAsync(mGotInventoryListener);
    }
}

@Override
protected void onStart() {
    super.onStart();
    queryPurchasedItems();
}

@Override
protected void onResume() {
    super.onResume();
    queryPurchasedItems();
    isListEmpty();
}

Your OnIabSetupFinishedListener checks for (!result.isSuccess())

but you don't return or abort here in case the resulst is NOT successful.

I think your mHelper.startSetup fails ,and you are not handling this failure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!