我正在尝试向我的活动添加ProgressDialog
以显示结算请求何时加载 - 因为它是网络请求,有时需要一段时间。我显示了进度对话框,然后在网络请求完成时将其关闭。然而,有时它需要很长时间并且似乎挂起。 如果我希望用户能够取消请求并关闭进度对话框,我将如何添加该功能?或者,是否有更好的方法来处理网络请求延迟?
这是我的购买代码:
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();
}
答案 0 :(得分:1)
mProgressDialog.setCancelable(true);
ProgressDialog#setOnCancelListener(...)
将OnCancelListener设置为ProgressDialog(http://developer.android.com/reference/android/app/Dialog.html#setOnCancelListener(android.content.DialogInterface.OnCancelListener))