我在存储中有复制文件的功能。当我单击以复制Button
时,将执行方法复制操作。这个方法如下:
public void actionCopy() {
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.progress_layout);
TextView progressTitle = (TextView) dialog.findViewById(R.id.progress_title);
progressTitle.setText("Copying...");
final ProgressBar progressBar = (ProgressBar) dialog.findViewById(R.id.progress_bar);
progressBar.setMax(listData.size());
progressBar.setProgress(0);
dialog.show();
for(int i = 0; i < listData.size(); i++) {
String value = new CopyDataUtils(listData.get(i)).execute().get();
progressBar.setProgress(i + 1);
Log.i(TAG, value);
}
}
CopyDataUtils AsyncTask:
public class CopyDataUtils extends AsyncTask<Void, Void, String> {
private DataItem item;
public CopyDataUtils(DataItem item) {
this.item = item;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
//...code copy file here
return ...;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
所以我的问题是:当actionCopy执行时,我想要显示对话框。但没有发生。执行for(..)
后,显示对话框。
出了什么问题?在执行多线程复制数据之前是否有关于show对话框的建议?
对不起,我的英文不好!
**更新:
我尝试将对话框添加到AsyncTask并将其显示在方法onPreExecute
中。但没有任何改变。
答案 0 :(得分:1)
问题是new CopyDataUtils(listData.get(i)).execute().get();
。 It blocks the UI thread and waits if necessary for the computation to complete, and then retrieves its result
您使用AsyncTask
和对话框的方式完全错误。您应该将这些代码移至AsynTask
。在onPreexecute
上,您应该显示对话框。 doInbackground
你做任务并在onPostexecute
隐藏对话。
答案 1 :(得分:0)
你应该在Asynctask的onPreEcecute()中显示进度条
public void onPreExecute()
{
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("your content");
progressDialog.setMax(listData.size());
progressDialog.setProgress(0);
for(int i = 0; i < listData.size(); i++) {
String value = new CopyDataUtils(listData.get(i)).execute().get();
Log.i(TAG, value);
progressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
return ...;
}
@Override
public void onPostExecute(String s)
{
progressBar.setProgress(i + 1);
}
答案 2 :(得分:0)
您应该将ProgressBar
作为全局变量,然后更新AsyncTask onPostExcute()
中的进度
public void YouActiviy() {
ProgressBar progressBar;
int i = 0;
public onCreate(){
....
}
public void actionCopy() {
...
progressBar = (ProgressBar) dialog.findViewById(R.id.progress_bar);
...
dialog.show();
for(int i = 0; i < listData.size(); i++) {
new CopyDataUtils(listData.get(i)).execute();
//progressBar.setProgress(i + 1);
}
}
public class CopyDataUtils extends AsyncTask<Void, Void, String> {
...
@Override
protected void onPostExecute(String s) {
// update your progressbar here
progressBar.setProgress(i++);
super.onPostExecute(s);
}
}
}
为方便起见,您可以按照tutorial这样与AsyncTask
ProgressBar
的正确方法
希望这个帮助
答案 3 :(得分:0)
你应该在onProgressUpdate()中设置进度,试试这个:
protected ProgressDialog mProgressDialog;
private class copyData extends AsyncTask<String, Integer, String> {
protected String doInBackground(Void... params) {
//copy data here
return ...;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(progress[0]);
}
protected void onPostExecute(String result) {
try {
if ((mProgressDialog != null) && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
} catch (final IllegalArgumentException e) {
// Handle or log or ignore
} finally {
mProgressDialog = null;
}
}
}