コード例 #1
0
  /**
   * 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");
    asyncFlag.start("refresh inventory");
    (new Thread(
            new Runnable() {
              public void run() {
                Result result =
                    new Result(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
                Inventory inv = null;
                try {
                  inv = queryInventory(querySkuDetails, moreSkus);
                } catch (BillingException ex) {
                  result = ex.getResult();
                }

                asyncFlag.end();

                final Result result_f = result;
                final Inventory inv_f = inv;
                handler.post(
                    new Runnable() {
                      public void run() {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                      }
                    });
              }
            }))
        .start();
  }
コード例 #2
0
  public void launchPurchaseFlow(
      Activity activity,
      String sku,
      int requestCode,
      OnPurchaseFinishedListener listener,
      String extraData) {
    checkSetupDone("launchPurchaseFlow");
    asyncFlag.start("launchPurchaseFlow");
    Result result;

    try {
      Bundle buyIntentBundle = connection.getBuyIntent(sku, extraData);
      int response = getResponseCodeFromBundle(buyIntentBundle);
      if (response != BILLING_RESPONSE_RESULT_OK) {
        result = new Result(response, "Unable to buy item");
        if (listener != null) listener.onPurchaseFinished(result, null);
      } else {
        PendingIntent intent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
        this.requestCode = requestCode;
        purchaseListener = listener;
        activity.startIntentSenderForResult(
            intent.getIntentSender(), requestCode, new Intent(), 0, 0, 0);
      }
    } catch (IntentSender.SendIntentException e) {
      result = new Result(HELPER_SEND_INTENT_FAILED, "Failed to send intent.");
      if (listener != null) listener.onPurchaseFinished(result, null);
    } catch (RemoteException e) {
      result = new Result(HELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
      if (listener != null) listener.onPurchaseFinished(result, null);
    }
  }