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