在Android中显示一定时间的ProgressBar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12691484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show ProgressBar for a certain time in Android
提问by Mokkapps
I have to wait some seconds in my Android App and I want to show a progress bar during this time, how can I do this?
我必须在我的 Android 应用程序中等待几秒钟,我想在此期间显示进度条,我该怎么做?
I tried for example this code:
我试过例如这个代码:
public boolean WaitTask() {
pDialog = ProgressDialog.show(context,null, "L?dt..",true);
new Thread() {
public void run() {
try{
// just doing some long operation
sleep(2000);
} catch (Exception e) { }
pDialog.dismiss();
}
}.start();
return true;
}
But the progressbar closes immediately without waiting the two seconds. Where is my problem?
但是进度条会立即关闭,而无需等待两秒钟。我的问题在哪里?
The progressbar should look like the activity circle showing in thissite from Android Developers.
进度条应类似于Android 开发人员在此站点中显示的活动圈。
UPDATEThe AsyncTask
更新异步任务
private class WaitTime extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog.show();
}
protected void onPostExecute() {
mDialog.dismiss();
}
@Override
protected void onCancelled() {
mDialog.dismiss();
super.onCancelled();
}
@Override
protected Void doInBackground(Void... params) {
long delayInMillis = 2000;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
mDialog.dismiss();
}
}, delayInMillis);
return null;
}
}
I call it like this:
我这样称呼它:
mDialog = new ProgressDialog(CreateProject.this);
mDialog = ProgressDialog.show(context,null, "L?dt..",true);
WaitTime wait = new WaitTime();
wait.execute();
回答by Ber?ák
I reccomend you to use AsyncTask, then you can do something like this:
我建议你使用 AsyncTask,然后你可以做这样的事情:
AsyncTask<Void, Void, Void> updateTask = new AsyncTask<Void, Void, Void>(){
ProgressDialog dialog = new ProgressDialog(MyActivity.this);
@Override
protected void onPreExecute() {
// what to do before background task
dialog.setTitle("Loading...");
dialog.setMessage("Please wait.");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// do your background operation here
return null;
}
@Override
protected void onPostExecute(Void result) {
// what to do when background task is completed
dialog.dismiss();
};
@Override
protected void onCancelled() {
dialog.dismiss();
super.onCancelled();
}
};
updateTask.execute((Void[])null);
and if you want to wait for some specific time, maybe you would like to use Timer:
如果您想等待某个特定时间,也许您想使用 Timer:
final ProgressDialog dialog = new ProgressDialog(MyActivity.this);
dialog.setTitle("Loading...");
dialog.setMessage("Please wait.");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
long delayInMillis = 5000;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
dialog.dismiss();
}
}, delayInMillis);
回答by Tal Kanel
mistake: calling pDialog.dismiss();
should be done from the UI thread instead of called from your new thread.
错误:pDialog.dismiss();
应该从 UI 线程调用而不是从新线程调用。
so your code should change to:
所以你的代码应该改为:
pDialog = ProgressDialog.show(context,null, "L?dt..",true);
new Thread() {
public void run() {
try{
// just doing some long operation
Thread.sleep(2000);
} catch (Exception e) { }
// handle the exception somehow, or do nothing
}
// run code on the UI thread
mYourActivityContext.runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.dismiss();
}
});
}.start();
generally - there are much better approaches performing background tasks (waiting and do nothing for two seconds is also background task) and performing something in the main UI thread when they finished. you can use AsyncTask
class for example. it's better use this android built in mechanism, and not "primitive" thread creation, although it will work too - only if you will handle right your application and activity life-cycle. remember there is a chance that in the two seconds you are waiting - the user can navigate away from your application. in that case the dismiss();
method would be call on a destroyed context...
通常 - 有更好的方法来执行后台任务(等待两秒钟不做任何事情也是后台任务)并在完成后在主 UI 线程中执行某些操作。AsyncTask
例如,您可以使用类。最好使用这个 android 内置机制,而不是“原始”线程创建,尽管它也可以工作 - 只有当您正确处理您的应用程序和活动生命周期时。请记住,有可能在您等待的两秒钟内 - 用户可以离开您的应用程序。在这种情况下,该dismiss();
方法将在销毁的上下文中调用...
I suggest you read more in - http://developer.android.com/reference/android/os/AsyncTask.html
我建议你阅读更多 - http://developer.android.com/reference/android/os/AsyncTask.html