更新android中的进度条
我正在活动中启动一个线程并立即显示进度对话框。该线程中的方法在完成其任务后调用其他线程,该线程调用 m_progressDialog.dismiss();
方法。
这工作正常,但我现在想实现进度条,该进度条如何根据上一个线程中的任务完成情况填充。完成其操作需要无限的时间。
这听起来可能模棱两可,但如果需要的话,我准备进一步详细说明。
一个小代码片段:
第一个线程中的方法像这样结束
...
runOnUiThread(m_returnRes);
}
private Runnable m_returnRes = new Runnable() {
public void run() {
m_progressDialog.dismiss();
我也尝试这样做
m_progressDialog = new ProgressDialog(this);
m_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
m_progressDialog.setProgress(0);
m_progressDialog.setMax(100);
m_progressDialog.setMessage("Retrieving data..");
m_progressDialog.show();
Thread background = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {while(m_progressDialog.getProgress()<=100){
Thread.sleep(500);}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
background.start();
,但栏没有被填充,并且在调用 .dismiss() 方法后直接被解雇
I am starting a thread in my activity and immediately displaying progress dialog.The method in this thread after completing its task calls other thread which calls m_progressDialog.dismiss();
method.
This works fine but i want to implement progress bar now how does this bar fills according to completion of task in previous thread.It takes indefinite amount of time for completing its operations.
this could sound ambigious but iam ready to elaborate it further if required.
A little code snippet:
Method in first thread ends like this
...
runOnUiThread(m_returnRes);
}
private Runnable m_returnRes = new Runnable() {
public void run() {
m_progressDialog.dismiss();
Also i've tried to do it this way
m_progressDialog = new ProgressDialog(this);
m_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
m_progressDialog.setProgress(0);
m_progressDialog.setMax(100);
m_progressDialog.setMessage("Retrieving data..");
m_progressDialog.show();
Thread background = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {while(m_progressDialog.getProgress()<=100){
Thread.sleep(500);}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
background.start();
but the bar does not gets filled and gets dismissed directly after .dismiss() method is called
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 AsyncTask。它为自己处理线程,并在 onProgressUpdate 更改视图内容,您可以在其中更新进度栏。
Try using AsyncTask. It handles threding for itself and has function publishprogress inside the onProgressUpdate to change view content, where you could update progress bar.