0

I am trying to add a ProgressDialog to my Activity to show when the billing request is loading -- since it is a network request, it is taking a while sometimes. I show the progress dialog, then I dismiss it when the network request is finished. However, sometimes it takes a REALLY long time and seems to hang. If I want the user to be able to cancel the request and dismiss the Progress Dialog, how would I go about adding that ability? Or, is there a better way to handle the network request lag?

Here is my purchasing code:

private void MakePurchase(final String s)
{
    if (makingPayment) return;
    makingPayment = true;

    mProgressDialog = new ProgressDialog(DonateActivity.this);
    mProgressDialog.setTitle("Google Play Store");
    mProgressDialog.setMessage("Loading...");
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setCancelable(false);
    mProgressDialog.setCanceledOnTouchOutside(false);
    mProgressDialog.show();

    new Thread(new Runnable() {
        public void run() {
            ArrayList<String> skuList = new ArrayList<String> ();
            skuList.add(s);
            Bundle querySkus = new Bundle();
            querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
            Bundle skuDetails;
            try {
                //Network request
                skuDetails = mService.getSkuDetails(3,
                        getPackageName(), "inapp", querySkus);
                mProgressDialog.dismiss();

                int response = skuDetails.getInt("RESPONSE_CODE");
                if (response == 0) {
                    ArrayList<String> responseList
                            = skuDetails.getStringArrayList("DETAILS_LIST");

                    for (String thisResponse : responseList) {
                        JSONObject object = new JSONObject(thisResponse);
                        String sku = object.getString("productId");
                        if (sku.equals(s)) {
                            Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
                                    sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                            PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
                            startIntentSenderForResult(pendingIntent.getIntentSender(),
                                    1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
                                    Integer.valueOf(0));
                        }
                    }
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
Adam
  • 488
  • 6
  • 19

1 Answers1

1
Chris Arriola
  • 1,636
  • 3
  • 17
  • 23
  • Yes, but how does one cancel a `mService.getSkuDetails()` call? – Adam Mar 24 '14 at 23:50
  • What is the underlying networking library you are using to perform the `mService.getSkuDetails()`? Apache HTTP? There's several ways of doing it, `mService.getSkuDetails()` can return some sort of network request entity that has a cancel method. Right now you don't have a reference to the pending request so there's no way to cancel the pending request until you expose some sort of canceling mechanism. – Chris Arriola Mar 25 '14 at 00:00
  • I'm just using the `IInAppBillingService` lib. I'm very new to the networking, and I'm not sure how I would expose the entity. Can you provide some example code? – Adam Mar 25 '14 at 00:11
  • Adam, did you ever figure this out? I'm in the same boat right now. – randallmeadows Jun 19 '15 at 14:35