如何在单声道Android版中使用ASYNCTask更新onProgress更新的进度

时间:2013-03-08 02:48:19

标签: android for-loop mono android-asynctask

我是Android编程的新手。我使用Mono for android ..我想制作一个progressdialog,显示我导入数据时的进度。我使用Asynctask ..任何人都可以帮助我如何使我的进度对话框显示进度。或者是否有任何其他可能的方法建议。

public class importData : AsyncTask
{

private ProgressDialog _progressDialog;
WebReference.Service1 service = new WebReference.Service1();
private Context _context;




public importData(Context context)

{

_context = context;



}

protected override void OnPreExecute()

{
// i make a horizontal progressdialog, i want to see the progress while importing

base.OnPreExecute();
_progressDialog = new ProgressDialog(_context) { Indeterminate = false };
_progressDialog.SetMessage("Please wait...");
 _progressDialog.SetProgressStyle(ProgressDialogStyle.Horizontal);
_progressDialog.Max = 806;
_progressDialog.Progress = 0;
_progressDialog.Show();

}

protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)

{
try
{
//My background Code in Importing Data
return true;
}
catch (Exception ex)
{
//When has an error return false
return false;
}


}

protected void OnProgressUpdate()
{
base.OnProgressUpdate();
//What i'm going to put in this part

}

protected override void OnPostExecute(Java.Lang.Object result)

{
base.OnPostExecute(result);
_progressDialog.Hide();
_progressDialog.Dismiss();
bool dd = (bool)result;
if (dd)
{


//Toast.MakeText(this, "Importing data completed", ToastLength.Short).Show();
Android.App.AlertDialog.Builder builder = new AlertDialog.Builder(_context);
AlertDialog ad = builder.Create();
ad.SetTitle("Info");
ad.SetIcon(Android.Resource.Drawable.IcDialogInfo);
ad.SetMessage("Importing deals completed");
ad.SetButton("OK", (s, e) => { });
ad.Show();
}
else {

Android.App.AlertDialog.Builder builder = new AlertDialog.Builder(_context);
AlertDialog ad = builder.Create();
ad.SetTitle("Error");
ad.SetIcon(Android.Resource.Drawable.IcDialogAlert);
ad.SetMessage("Failed");
ad.SetButton("OK", (s, e) => { });
ad.Show();
}
}


}

1 个答案:

答案 0 :(得分:0)

我在下面修改了你的代码。希望能帮助到你。或者给你一个想法。在DoinBackground中循环你的progressdialog并使用this.publishProgress将它传递给onprogressupdate ..

public class importData : AsyncTask
{

private ProgressDialog _progressDialog;
WebReference.Service1 service = new WebReference.Service1();
private Context _context;




public importData(Context context)

{

_context = context;



}

protected override void OnPreExecute()

{
// i make a horizontal progressdialog, i want to see the progress while importing

base.OnPreExecute();
_progressDialog = new ProgressDialog(_context) { Indeterminate = false };
_progressDialog.SetMessage("Please wait...");
 _progressDialog.SetProgressStyle(ProgressDialogStyle.Horizontal);
_progressDialog.Max = 806;
_progressDialog.Progress = 0;
_progressDialog.Show();

}

protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)

{

try
{
for(int i=0; i<_progressDialog.Max; i++) {
    this.PublishProgress(i);
}
//My background Code in Importing Data
return true;
}
catch (Exception ex)
{
//When has an error return false
return false;
}


}



protected override void OnProgressUpdate(params Java.Lang.Object[] values)
{
base.OnProgressUpdate();
//increment the progress of your progrssdialog
_progressDialog.IncrementProgressBy(1);

}

protected override void OnPostExecute(Java.Lang.Object result)

{
base.OnPostExecute(result);
_progressDialog.Hide();
_progressDialog.Dismiss();
bool dd = (bool)result;
if (dd)
{


//Toast.MakeText(this, "Importing data completed", ToastLength.Short).Show();
Android.App.AlertDialog.Builder builder = new AlertDialog.Builder(_context);
AlertDialog ad = builder.Create();
ad.SetTitle("Info");
ad.SetIcon(Android.Resource.Drawable.IcDialogInfo);
ad.SetMessage("Importing deals completed");
ad.SetButton("OK", (s, e) => { });
ad.Show();
}
else {

Android.App.AlertDialog.Builder builder = new AlertDialog.Builder(_context);
AlertDialog ad = builder.Create();
ad.SetTitle("Error");
ad.SetIcon(Android.Resource.Drawable.IcDialogAlert);
ad.SetMessage("Failed");
ad.SetButton("OK", (s, e) => { });
ad.Show();
}
}


}