我想在执行函数时实现进度条。这将帮助我通知用户正在进行更新。 但我无法看到任何进度条,而是将Logcat消息视为progressBar维度。
class Async extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute()
{
ProgressDialog progress=new ProgressDialog(App_list_Activity.this);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
@Override
protected String doInBackground(Void... arg0)
{
updateDatabase();
return null;
}
@Override
protected void onPostExecute(String result)
{
ProgressDialog progress=new ProgressDialog(App_list_Activity.this);;
progress.dismiss();
}
}
new Async().execute();
我哪里错了?
答案 0 :(得分:0)
在课程级别声明ProgressDialog:
class Async extends AsyncTask<Void, Void, String> {
ProgressDialog progress;
在onPreExecute()中初始化:
protected void onPreExecute() {
progress=new ProgressDialog(App_list_Activity.this);
在onPostExecute()中解雇:
protected void onPostExecute(String result){
progress.dismiss();
在你的代码中,你再次在onPostExecute()中初始化ProgressDialog以解除onPreExecute ProgressDialog,这样它将创建新的ProgressDialog实例并将其解除但仍然是onPreExecute()ProgressDialog不会被解雇。
示例:
class Async extends AsyncTask<Void, Void, String> {
ProgressDialog progress;
@Override
protected void onPreExecute()
{
progress=new ProgressDialog(App_list_Activity.this);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
@Override
protected String doInBackground(Void... arg0)
{
updateDatabase();
return null;
}
@Override
protected void onPostExecute(String result)
{
progress.dismiss();
}
}
new Async().execute();
答案 1 :(得分:0)
正如Android文档所述:
onProgressUpdate(Progress ...),在调用publishProgress(Progress ...)后在UI线程上调用。执行的时间是不确定的。此方法用于在后台计算仍在执行时显示用户界面中的任何形式的进度。例如,它可用于为进度条设置动画或在文本字段中显示日志。
答案 2 :(得分:0)
试试这个:
private ProgressDialog progress;
class Async extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute()
{ progress= new ProgressDialog(SingleContactActivity.this);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
@Override
protected String doInBackground(Void... arg0)
{
updateDatabase();
return null;
}
@Override
protected void onPostExecute(String result)
{
if (progress.isShowing())
progress.dismiss();
}
}
new Async().execute();
}
答案 3 :(得分:0)
您正在onPreExecute
中创建一个实例,在onPostExecute
中创建另一个实例。您应该保留该引用并将其解雇。
最重要的是,我建议让会员progress
保持最终状态,否则Android可能会对泄密感到尖叫。
此外,解雇对话框也应该在onCancelled
。
class Async extends AsyncTask<Void, Void, String> {
private final ProgressDialog progress;
public Async(Context c) {
super();
this.progress = new ProgressDialog(c);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
@Override
protected void onPreExecute() {
progress.show();
}
@Override
protected String doInBackground(Void... arg0) {
updateDatabase();
return null;
}
@Override
protected void onPostExecute(String result) {
if (progress.isShowing()) {
progress.dismiss();
}
super.onPostExecute(result);
}
@Override
protected void onCancelled() {
if (progress.isShowing()) {
progress.dismiss();
}
super.onCancelled();
}
}
new Async().execute();
HTH。