1

I refer to the following code from Google's in-app purchase sample code

/**
 * Asynchronous wrapper for inventory query. This will perform an inventory
 * query as described in {@link #queryInventory}, but will do so asynchronously
 * and call back the specified listener upon completion. This method is safe to
 * call from a UI thread.
 *
 * @param querySkuDetails as in {@link #queryInventory}
 * @param moreSkus as in {@link #queryInventory}
 * @param listener The listener to notify when the refresh operation completes.
 */
public void queryInventoryAsync(final boolean querySkuDetails,
                           final List<String> moreSkus,
                           final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            }
            catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            handler.post(new Runnable() {
                public void run() {
                    listener.onQueryInventoryFinished(result_f, inv_f);
                }
            });
        }
    })).start();
}

What make me feel doubt is that, why do we need a Handler to perform onQueryInventoryFinished callback? Why can't we just let the newly created Thread to execute the function directly?

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            listener.onQueryInventoryFinished(result_f, inv_f);

Unless the reason is that, we want to execute onQueryInventoryFinished using UI thread. However, that is not correct. The code sample is creating Handler through empty parameter constructor. As if we want to execute it using UI thread, we suppose to create our Handler through

Handler handler = new Handler(Looper.getMainLooper());

May I know, is there any particular reason to use Handler to perform callback in the above Google code sample?

1 Answer 1

1

A Handler will run/process Runnables and messages that you send to it on the thread that it was created on. It's a handy tool to do proper inter-thread communication by passing messages on Android.

From the code you pasted in, it's not clear that which thread executes this method. It may very well be the UI thread, in which case the created Handler will execute the passed Runnable on the UI thread. Otherwise, I think that the developer only wanted to make sure the the async task notifies the caller about its finished state on the same thread on which the task was started on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.