这是我的应用程序的流程:显示ProgressDialog直到的AsyncTask完成,然后更新UI
1)用户需要的图片或视频
2)将培养基保存在内部存储器
3)的路径被分配给该UI被更新,以指示用户可以继续
的UI更新只有如果C的自定义对象
4) ustom对象具有图像路径或视频路径。我刚开始使用AsyncTask
在后台线程中保存到内部存储器,这样在保存大文件时应用程序不会挂起,但我遇到了一些问题。
我想要做的:显示ProgressDialog
直到doInBackground()
完成,那么路径分配给我的对象,然后继续主线程上更新UI。
现在,主线程将继续运行,而AsyncTask
仍在工作,并且UI将不会正确更新,因为路径尚未分配给对象。
我已阅读约AsyncTask#get()
,但我不确定如何使用ProgressDialog
来实现它。我尝试了,主线程在继续之前似乎没有等待结果。
我非常感谢任何帮助。谢谢!
我AsyncTask
:
private class SaveMediaTask extends AsyncTask<Integer, Void, String>
{
private ProgressDialog progressDialog;
private int mediaType;
public SaveMediaTask()
{
progressDialog = new ProgressDialog(getActivity(), ProgressDialog.THEME_HOLO_LIGHT);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
this.progressDialog.setTitle("Processing");
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
this.progressDialog.show();
}
protected String doInBackground(Integer... mediaType)
{
//save to internal storage and return the path
return path;
}
protected void onPostExecute(String path)
{
//by the time this runs, the UI has already tried to update itself on the main thread,
//and found that myObject does not yet have a path. Once this runs, it is too late.
myObject.setPath(path);
if (progressDialog.isShowing())
{
progressDialog.dismiss();
}
}
}
我怎么称呼它,用户离开照相机后立即:
new SaveMediaTask().execute(MEDIA_TYPE_IMAGE);
//WAIT HERE AND DISPLAY PROGRESSDIALOG UNTIL TASK IS DONE
//update UI
你的任务,我不能真正得到你想要的东西,但这个答案吧[这里]( http://stackoverflow.com/questions/27499292/cant-show-alertdialog-in-doinbackground/27500068#27500068)会告诉你如何构建你的asynctask,同时注意你的代码需求,我不认为你需要检查直接从主线程的路径,我在想asynctask有办法可以到达用户界面,所以用它来检查你的对象路径,如果它真正的调用postexecute并做你的东西,希望它有帮助 – Elltz