0

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;
Rúben
  • 122
  • 2
  • 8
  • Can you explain what exactly you're hoping to achieve? You are returning `null` from `doInBackground`, so `null` will be passed into `onPostExecute`. What exactly are you expecting to happen? – ci_ Feb 25 '15 at 23:06
  • i want to update the mPremiumV value (true or false) and the mAdds value (true or false) depending on the query called by mHelper.queryInventoryAsync(mGotInventoryListener); on doInBackground – Rúben Feb 25 '15 at 23:10

2 Answers2

1

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;
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • This helped me just needed one correction. Now i noticed that my problem is that the values uptated when called mHelper.queryInventoryAsync(mGotInventoryListener); dont come back. But this is another question. – Rúben Feb 26 '15 at 23:51
  • Great! What was the correction that you had to make? Feel free to edit, or let me know and I'll edit. – Daniel Nugent Feb 26 '15 at 23:55
  • i needed to replace `public Result doInBackground` for `public ArrayList doInBackground` – Rúben Feb 26 '15 at 23:58
1

Like some users comment, from doInBackground() you are returning a null value that will be passed into onPostExecute().

Based on your code, your method doInBackground() must return a value of type "Result"

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

        Result res;
...
...
...

        return res;
    }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268