使用inappbilling的信息没有从doInbackground传递到postexecute

时间:2015-02-25 22:57:38

标签: android android-asynctask in-app-billing android-fragmentactivity

当我的应用程序启动时,我启动AsyncTask,以便立即显示用户高级功能。但我没有从doInBackground(查询后的值)传递信息到onPostExecute。

我执行queryInventoryAsync定义值为true或false然后我将片段上的这些值用于不同的操作。

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) {

    }

}

然后在片段中调用

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

if (mPremiumV) {
//open a fragment 

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

2 个答案:

答案 0 :(得分:1)

最初我很难弄清楚你在这里要做什么,但我认为这可能会有所帮助:

快速审核变更:

 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) {

片段:

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);

把它们放在一起:

     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) {

    }

}

片段代码:

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;

答案 1 :(得分:1)

与某些用户评论一样,您从doInBackground()返回一个空值,该值将传递到onPostExecute()

根据您的代码,您的方法doInBackground()必须返回类型&#34;结果&#34;

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

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

        return res;
    }