Anu*_*nuj 0 android progressdialog android-asynctask
我已经创建了一个应用程序,其中用户登录他的帐户.为此我使用asyncTask.Everything工作正常,但事情是当我得到响应,我希望进度条停止.但它继续下去.
异步任务
protected void onPreExecute() {
super.onPreExecute ();
CommonFunctions.showProgress (c, "Please Wait", true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute (s);
try {
JSONObject jsonObject = new JSONObject (s.trim ());
JSONObject NewDataSet = jsonObject.getJSONObject ("NewDataSet");
JSONObject Table = NewDataSet.getJSONObject ("Table");
String User_ID = Table.getString ("User_ID");
String Vendor_IEntity_Code = Table.getString ("Vendor_IEntity_Code");
String Vendor_Name = Table.getString ("Vendor_Name");
// setting the preferences
SettingPreference.setUserId (c, User_ID);
SettingPreference.setVendorId (c, Vendor_IEntity_Code);
SettingPreference.setVendorName (c, Vendor_Name);
} catch (JSONException e) {
e.printStackTrace ();
}
CommonFunctions.showProgress (c, "", false);
Crouton.makeText ((android.app.Activity) c, "Login Sucessful", Style.CONFIRM).show ();
}
@Override
protected String doInBackground(String... strings) {
response = HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/IsUserValid").send ("_UserID=" + strings[0] + "&_Password=" + strings[1]).body ();
Log.e ("Login Response", "" + response);
return response;
}
Run Code Online (Sandbox Code Playgroud)
CommonFunctions
public class CommonFunctions {
private Context c;
public static void showProgress(Context context, String message, boolean isVisible) {
ProgressDialog progressDialog = new ProgressDialog (context);
progressDialog.setMessage (message);
progressDialog.setCancelable (false);
if (isVisible) {
progressDialog.show ();
} else if (isVisible == false) {
if (progressDialog.isShowing ()) {
progressDialog.dismiss ();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
ProgressDialog progressDialog = new ProgressDialog (context);
Run Code Online (Sandbox Code Playgroud)
因此,每次调用showProgress方法时,您都在创建一个新方法,ProgressDialog因此在再次调用该方法时不会解雇.
解:
仅创建一次实例 ProgressDialog
public class CommonFunctions {
private Context c;
ProgressDialog progressDialog;
public static void showProgress(Context context, String message, boolean isVisible) {
if(progressDialog == null)
{
progressDialog = new ProgressDialog (context);
progressDialog.setMessage (message);
progressDialog.setCancelable (false);
}
if (isVisible) {
progressDialog.show();
} else if (isVisible == false) {
if (progressDialog.isShowing ()) {
progressDialog.dismiss();
progressDialog = null;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
327 次 |
| 最近记录: |