Java AsyncTask progressdialog未显示,doInBackground块UI
我一直试图在检索数据时让progressdialog正常工作 我用这个链接开始,但似乎我不能让它正常工作 到目前为止,它没有显示任何progressbar,因为我认为函数fillMyTickets会阻塞UI线程,因此对话框不会显示。我不明白如何解决这个问题。通过阅读Stackoverflow,我找到了一些解决方案,比如在AsyncTask中使用onProgressUpdate函数,但这不是我想要的。我所需要的只是一个等待的对话框,上面写着HTTP调用结束时下载并消失 exampleFragment.java中的Asynctask函数Java AsyncTask progressdialog未显示,doInBackground块UI,java,android,android-asynctask,Java,Android,Android Asynctask,我一直试图在检索数据时让progressdialog正常工作 我用这个链接开始,但似乎我不能让它正常工作 到目前为止,它没有显示任何progressbar,因为我认为函数fillMyTickets会阻塞UI线程,因此对话框不会显示。我不明白如何解决这个问题。通过阅读Stackoverflow,我找到了一些解决方案,比如在AsyncTask中使用onProgressUpdate函数,但这不是我想要的。我所需要的只是一个等待的对话框,上面写着HTTP调用结束时下载并消失 exampleFragmen
private ProgressDialog dialog;
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Downloading..");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
fillMyTickets();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
}
private void fillMyTickets() {
HttpReader httpReader = new HttpReader();
httpReader
.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
@Override
public void resultReady(String result) {
JsonHelper jsonHelper = new JsonHelper();
tickets = jsonHelper.getTickets(result);
}
});
httpReader
.execute(url);
}
onAttach(活动活动)执行异步任务的函数
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
new PrefetchData().execute();
}
另外,在AsyncTask的onPostExecute中注释dialog.disclose()时,对话框确实会显示,但当然永远不会关闭。好的,所以我误读了这个问题。。。。但是后台不应该阻挡任何东西,这就是为什么它在后台 不,这就是答案。。。。对话框在ui上不正确。只需从常规活动而不是异步任务运行它。我用你的女朋友就是这样,而且很漂亮
public class MainActivity extends Activity {
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new ProgressDialog(this);
dialog.setMessage("Downloading..");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
因此,基本上将对话框本身移出asynctask,并使用“this”作为上下文。如果您将访问权限从私有更改为公共,则从应用程序中的任何位置启动或终止它都不会有问题您正在调用一个注册侦听器并下载一些数据的方法,但在下载数据之前,您将从
后台返回并取消该对话框,因此,您的对话框将被显示,而隐藏。尝试修改代码并在完成下载时关闭对话框:
private ProgressDialog dialog;
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Downloading..");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpReader httpReader = new HttpReader();
httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
@Override
public void resultReady(String result) {
JsonHelper jsonHelper = new JsonHelper();
tickets = jsonHelper.getTickets(result);
//finished the download and we dismiss the dialog
dialog.dismiss();
}
});
httpReader.execute(url);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
私有进程对话框;
私有类预取数据扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
dialog=newprogressdialog(getActivity());
setMessage(“下载…”);
对话框。setUndeterminate(true);
对话框。可设置可取消(true);
dialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpReader HttpReader=新HttpReader();
httpReader.setOnResultReadyListener(新的httpReader.OnResultReadyListener(){
@凌驾
公共无效结果(字符串结果){
JsonHelper JsonHelper=新的JsonHelper();
tickets=jsonHelper.getTickets(结果);
//下载完毕,我们关闭对话框
dialog.dismise();
}
});
httpReader.execute(url);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
}
希望它能有所帮助!我建议您使用Volley库来执行异步网络请求(包含重试策略、线程池执行器以及其他一些您可以扩展和修改的功能),在过去的项目中,我使用了一个带有进度对话框的异步任务,以前也遇到过这个问题。我处理这个问题的方法是将回调接口写入到扩展AsyncTask的类中,该类将在postExecute()上调用
公共类MyDataGrabber扩展异步任务{
公共接口回调{
未完成的公共void(字符串数据);
}
公开回调;
公共MyDataGrabber(回调){//初始化回调
this.callBack=回调;
}
//论预执行
//在后台
public void onPostExecute(字符串数据){
this.callBack.onComplete(数据);
}
}
//因此,当您调用异步任务时,它将如下所示
对话;
//..设置并显示对话框等。
新建MyDataGrabber(新建MyDataGrabber.Callback()){
@凌驾
公共void onComplete(字符串数据){
//处理数据
doDataHandling();
//取消对话框
if(dialog.isShowing){
dialog.dismise();
}
}
}).执行(字符串url…其他数据等);
Salauyou是正确的,尽管AsyncTask的缺点是正确的,但它并行或串行执行时会产生一些负面的副作用,这取决于android api级别以及并发问题,即使在关闭应用程序后,异步任务仍在后台运行。通常情况下,它要求您编写各种取消任务正确处理方法和弱引用。使用线程和处理程序肯定也是一个值得的解决方案。我并不是想让splashscreen正常工作,我只使用了指南的最后一部分。我试图在下载票证并将其放入列表视图时显示一个对话框。而且,当您唱我上面的代码。这就是为什么我认为UI被阻止的原因,因为对话框在后台操作前后被打开和关闭。dialog
位于UI线程上,如onPreExecute()所示
它确实在UI线程上运行
。是的,但严格来说我不必这么说……我不是在争论……只是说你可以通过将抓取保留在异步中,并将对话框移动到主活动来实现你想要的,把它当作一个奇妙的祝酒词,你不需要为Dialogy执行异步任务,你说,“对话框在ui上不是很准确”,但现在,“是的,他的对话框是”。无论如何,如果这样做,那么就不需要异步任务
…只需使用线程
这是一个非常漂亮的对话框。顺便说一句,你能展示一下你是如何执行任务的吗?确保你在ui线程上调用新的预取数据().execute()
(可能在一个onClick
处理程序中,然后你就应该得到你想要的。它必须自动加载doInBackground(Void…arg0)上的error,并使用“此方法必须返回Void类型的结果”另外,许多进度对话框可以使用setSupportProgressBarIn在actionbar中处理
public class MyDataGrabber extends AsyncTask<Void,Void,Void> {
public interface Callback {
public void onComplete(String data);
}
public Callback callBack;
public MyDataGrabber(Callback callback) { // Initialize the callback
this.callBack = callback;
}
// on preExecute
// on doInBackground
public void onPostExecute(String data){
this.callBack.onComplete(data);
}
}
// So when you call your async task it will look like this
Dialog dialog;
//.... set up and show dialog etc.
new MyDataGrabber(new MyDataGrabber.Callback(){
@Override
public void onComplete(String data){
// Handle data
doDataHandling();
// Dismiss dialog
if(dialog.isShowing){
dialog.dismiss();
}
}
}).execute(String url...other data etc);