看不到ProgressDialog而AsyncTask的在后台运行 [英] Can't see ProgressDialog while AsyncTask run in background

查看:117
本文介绍了看不到ProgressDialog而AsyncTask的在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序使用的AsyncTask下载的URL。我上preExecute使用ProgressDialog()等待。 但我不能看到ProgressDialog而流程结束,我看到了一会儿。想看到它,而在那之后下载不行。 任何一个可以帮助我。 谢谢 我的code是这样的:

  

 私有类loadMoreListView扩展的AsyncTask<虚空,虚空,虚空> {

    @覆盖
    在preExecute保护无效(){
        //显示进度对话框发送HTTP请求之前
        pDialog =新ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage(请稍候...);
        pDialog.isIndeterminate();
        pDialog.setCancelable(假);
        pDialog.show();
    }

    保护无效doInBackground(空...未使用){
        runOnUiThread(新的Runnable(){
            公共无效的run(){

           //做点什么下载

        }
        });

        返程(空);
    }


    保护无效onPostExecute(作废不用){
        //关闭进度对话框
        pDialog.dismiss();
    }
}
 

解决方案

首先发现连接到所有的AsyncTask的实现方法@覆盖头如

 私有类loadMoreListView扩展的AsyncTask<虚空,虚空,虚空> {

    ProgressDialog pDialog;

    @覆盖
    在preExecute保护无效(){
        // TODO自动生成方法存根
        super.on preExecute();
        pDialog =新ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage(请稍候...);
        pDialog.isIndeterminate();
        pDialog.setCancelable(假);
        pDialog.show();
    }

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        // TODO自动生成方法存根

        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效的结果){
        // TODO自动生成方法存根
        super.onPostExecute(结果);
        pDialog.cancel();

    }

}
 

另外从doInBackground删除,除非你必须做一些事情上的用户界面。

  runOnUiThread(新的Runnable(){
        公共无效的run(){

       //做点什么下载

    }
    });
 

您不能做一些下载的runOnUiThread。 doInBackground意指用于运行像下载等不可见的用户界面的后台任务。

I use AsyncTask in my App for download a url. I use a ProgressDialog on onPreExecute() for waiting. But I cant see ProgressDialog while process finish and i see it for a moment. want to see it while downloading not after that. can any one help me. thanks my code is like this:

private class loadMoreListView extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage("Please Wait ...");
        pDialog.isIndeterminate();
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected Void doInBackground(Void... unused) {
        runOnUiThread(new Runnable() {
            public void run() {

           // do something for downloading

        }
        });

        return (null);
    }


    protected void onPostExecute(Void unused) {
        // closing progress dialog
        pDialog.dismiss();
    }
}

解决方案

Firstly notice the "@override" header attached to all the AsyncTask Implemented methods e.g.

private class loadMoreListView extends AsyncTask<Void, Void, Void> {

    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(SingleMenuItemActivity.this);
        pDialog.setMessage("Please Wait ...");
        pDialog.isIndeterminate();
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pDialog.cancel();

    }

}

Also Remove this from doInBackground unless you must do something on the UI.

runOnUiThread(new Runnable() {
        public void run() {

       // do something for downloading

    }
    });

You cannot do something for downloading on the runOnUiThread. doInBackground is meant for running background tasks like downloads etc. not visible to the UI.

这篇关于看不到ProgressDialog而AsyncTask的在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆