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

Information not passing from doInbackground to postexecute using inappbilling

我在我的应用程序启动时启动一个 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;

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

快速查看更改:

 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;

就像一些用户评论的那样,从 doInBackground() 你正在 returning 一个空值,该值将被传递到 onPostExecute()

根据您的代码,您的方法 doInBackground() 必须 return 类型为 "Result"

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

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

        return res;
    }