Search code examples
androidandroid-asynctaskin-app-billingandroid-fragmentactivity

Information not passing from doInbackground to postexecute using inappbilling


I start an AsyncTask when my apps start so that user premium features will be show right away. But i am not getting info passed from doInBackground (the values after the query) to onPostExecute.

I do a queryInventoryAsync that defines values are true or false then I use this values on fragment to diverse actions.

public class LoadAppBilling extends AsyncTask <Result, Result, Result> {

    static final String SKU_PREMIUMV = "test.hsdbgjfasbdfughvakcshfgb";
    static final String SKU_NO_ADDS = "test.blah";
    static final String TAG = "Azores Bus Premium";
    IabHelper mHelper;
    boolean mPremiumV = false;
    boolean mAdds = false;

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

            if (result.isFailure()) {
                Log.d(TAG, "didnt load");
                return;
            }
            Log.d(TAG, " load");

            if (inventory.hasPurchase(SKU_PREMIUMV)) {
                         mPremiumV = true;
                return;
            }
            if (inventory.hasPurchase(SKU_NO_ADDS)) {
                         mAdds = true;
            }

        }
    };

    @Override
    public Result doInBackground(Result... params) {

        String base64EncodedPublicKey = "";

        Log.d(TAG, "Creating IAB helper.");
        mHelper = new IabHelper(getApplication(), base64EncodedPublicKey);

        mHelper.enableDebugLogging(true);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    Log.d(TAG, "Problem setting up In-app Billing: " + result);
                }
                // Hooray, IAB is fully set up!
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        });


        return null;
    }

    @Override
    public void onPostExecute(Result  result) {

    }

}

and then in fragment i call

new LoadAppBilling() {
         @Override
     public void onPostExecute (Result result) {
     super.onPostExecute(result);

if (mPremiumV) {
//open a fragment 

} else {    
// show a dialog
    }}
}
.execute();
break;

Solution

  • Initially I had a hard time figuring out what you're trying to do here, but I think this might help:

    Quick review of changes:

     public class LoadAppBilling extends AsyncTask <Result, Result, ArrayList<Boolean>> 
    

    .......

     ArrayList<Boolean> retVal = new ArrayList<Boolean>();
                    retVal.add(mPremiumV);
                    retVal.add(mAdds);
    
                    return retVal;
    

    .......

     @Override
        public void onPostExecute(ArrayList<Boolean>  result) {
    

    Fragment:

    public void onPostExecute (ArrayList<Boolean> result) {
             super.onPostExecute(result);
    
             //This will auto-unbox to boolean primitive
             boolean mPremiumV = result.get(0);
             boolean mAdds = result.get(1);
    

    Put it all together:

         public class LoadAppBilling extends AsyncTask <Result, Result, ArrayList<Boolean>> {
    
            static final String SKU_PREMIUMV = "test.hsdbgjfasbdfughvakcshfgb";
            static final String SKU_NO_ADDS = "test.blah";
            static final String TAG = "Azores Bus Premium";
            IabHelper mHelper;
            boolean mPremiumV = false;
            boolean mAdds = false;
    
            IabHelper.QueryInventoryFinishedListener mGotInventoryListener
                    = new IabHelper.QueryInventoryFinishedListener() {
                public void onQueryInventoryFinished(IabResult result,
                                                     Inventory inventory) {
    
                    if (result.isFailure()) {
                        Log.d(TAG, "didnt load");
                        return;
                    }
                    Log.d(TAG, " load");
    
                    if (inventory.hasPurchase(SKU_PREMIUMV)) {
                                 mPremiumV = true;
                        return;
                    }
                    if (inventory.hasPurchase(SKU_NO_ADDS)) {
                                 mAdds = true;
                    }
    
                }
            };
    
            @Override
            public ArrayList<Boolean> doInBackground(Result... params) {
    
                String base64EncodedPublicKey = "";
    
                Log.d(TAG, "Creating IAB helper.");
                mHelper = new IabHelper(getApplication(), base64EncodedPublicKey);
    
                mHelper.enableDebugLogging(true);
    
                mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                    public void onIabSetupFinished(IabResult result) {
                        if (!result.isSuccess()) {
                            // Oh noes, there was a problem.
                            Log.d(TAG, "Problem setting up In-app Billing: " + result);
                        }
                        // Hooray, IAB is fully set up!
                        mHelper.queryInventoryAsync(mGotInventoryListener);
                    }
                });
    
                ArrayList<Boolean> retVal = new ArrayList<Boolean>();
                retVal.add(mPremiumV);
                retVal.add(mAdds);
    
                return retVal;
            }
    
        @Override
        public void onPostExecute(ArrayList<Boolean>  result) {
    
        }
    
    }
    

    Fragment code:

    new LoadAppBilling() {
             @Override
         public void onPostExecute (ArrayList<Boolean> result) {
         super.onPostExecute(result);
    
         //This will auto-unbox to boolean primitive
         boolean mPremiumV = result.get(0);
         boolean mAdds = result.get(1);
    
    
    if (mPremiumV) {
    //open a fragment 
    
    } else {    
    // show a dialog
        }}
    }
    .execute();
    break;