Android AsyncTask如何使用progressbar
我想在执行函数时实现一个进度条。这将帮助我通知用户正在进行更新。 但我无法看到任何progressbar,而是将Logcat消息视为progressbar维度Android AsyncTask如何使用progressbar,android,Android,我想在执行函数时实现一个进度条。这将帮助我通知用户正在进行更新。 但我无法看到任何progressbar,而是将Logcat消息视为progressbar维度 class Async extends AsyncTask<Void, Void, String> { @Override protected void onPreExecute() {
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();
我哪里出错了?在类级别声明ProgressDialog:
class Async extends AsyncTask<Void, Void, String> {
ProgressDialog progress;
在onPostExecute中解除:
protected void onPostExecute(String result){
progress.dismiss();
在您的代码中,您再次在onPostExecute中初始化ProgressDialog以解除onPreExecute ProgressDialog,这样它将创建新的ProgressDialog实例并解除它,但onPreExecute ProgressDialog仍然没有解除
例如:
正如Android文档所述: onProgressUpdateProgress…,在调用publishProgressProgress…后在UI线程上调用。。。。执行的时间未定义。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可以用于制作进度条动画或在文本字段中显示日志。尝试以下操作:
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();
}
您正在onPreExecute中创建一个实例,在onPostExecute中创建另一个实例。你应该保留该引用,并删除该引用 除此之外,我建议保持会员进度为最终结果,否则安卓可能会尖叫泄漏 此外,discouse对话框也应该在onCancelled中
HTH.尝试解释他做错了什么以及如何纠正错误,而不是只放代码。我在我的mainActivity@HareshChhelana谢谢Naveen和Haresh但我看到唯一的功能是执行进度条没有显示我认为进度条是显示,但可能是由于onpreexecute和postexecute之间的时间间隔非常小,因此会被忽略……任何延长时间的选项。长度不需要在onPost中创建进度。删除它..只需使用progress.disclose;在OnPostExecuteAsync任务与mainactivity类分离时,上下文应该是什么。传入对您的Activity或ApplicationContext的引用;我修改了代码,将上下文作为构造函数参数。感谢您共享基本信息。我仍然无法看到进度条,因为我认为progress.show和progress.disclose时间间隔很小,可能是函数需要几秒钟才能执行,或者我尝试使用ifprogress.isShowing{progress.show}并且进度条是可见的。请给出一些解决方法现在这对我来说很有效。我添加了Thread.sleep10000;在doInBackground函数中尝试捕捉
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();
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();
}
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();