进度对话框异步任务花费比预期更长的时间 [英] Progress dialog async task taking longer time than expected

查看:185
本文介绍了进度对话框异步任务花费比预期更长的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android编程的新手.我正在开发一个Web爬网程序,正在为其使用异步任务并且运行良好.为了使用户了解最新情况,我正在使用进度对话框.我的问题是,如果我使用进度对话框,则我的程序需要花费更多的时间来执行,而当我不使用进度对话框时,它的执行速度会更快.

I am new to android programming. I am developing a web crawler for which i am using a Async Task and it is working well.In order to keep user informed,i am using progress dialog. My problem is,if i use a Progress Dialog my program takes more time to execute and when i won`t use the progress dialog,it executes faster.

完成工作 OnCreate方法

Done Work OnCreate Method

 protected void onCreate(Bundle savedInstanceState) {
    try {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);

        Intent intent = getIntent();

        s1 = intent.getStringExtra("Number1");
        s2 = intent.getStringExtra("Number2");
        s3=intent.getIntExtra("selectedItem",0);
        HttpAsyncTask asyncTask = new HttpAsyncTask();
        asyncTask.execute();


    }catch (Exception e)
    {
        messageBox("Exception",e.getMessage());
    }

}

异步任务类

 private class HttpAsyncTask extends AsyncTask<List<String>, Integer, List<String>> {
    private ProgressDialog dialog;
    @Override
    protected void onPreExecute() 
     {
        dialog = new ProgressDialog(Results.this);
        dialog.setIndeterminate(true);
        dialog.setMessage("Please Wait");
        dialog.setCancelable(true);
        dialog.show();
        super.onPreExecute();
    }
    @Override
    protected List<String> doInBackground(List<String>... urls) {
        //android.os.Debug.waitForDebugger();
       // spinner.setVisibility(View.VISIBLE);
        List<String>resultList=new ArrayList<String>();
        try
        {
            if(isCancelled())
                return resultList;

         resultList=WebCrawlerClass.GetPost(s1,s2,s3);

        }catch (Exception e)
            {
                messageBoxs("Error", e.getMessage());
            }
       return resultList;
    }


    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(List<String> result)
    {

    if(dialog.isShowing())
    {
     dialog.dismiss();
    }
        if(s3 == 2)
        {
            docListAdapter=new ListViewData(Results.this,result);
        }
        else {
            docListAdapter = new NameNumListData(Results.this, result);
        }

        docList=(ListView)findViewById(R.id.listView2);
        docList.setAdapter(docListAdapter);
        super.onPostExecute(result);
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        this.cancel(true);
    }
}

我错过了什么吗?需要帮助.

Am I missing something? Need help..

感谢和问候, 阿比纳夫

Thanks and Regards, Abhinav

推荐答案

活动中

//启动进度对话框 ..

// Start the progress dialog ..

Handler handler = new Handler() {
      @Override
       public void handleMessage(Message msg) {
       super.handleMessage(msg);
         // dismiss the progress dialog
      }
    };
 HttpAsyncTask asyncTask = new HttpAsyncTask(handler);
 asyncTask.execute();

在您的asynctask类中

private class HttpAsyncTask extends AsyncTask<List<String>, Integer, List<String>> {

    private Handler handler = null;
    public HttpAsyncTask (Handler handler) {
        this.handler = handler;
    }

    protected Void doInBackground(Void... params) {
       //Perform your task
       // When you know that task is finished , fire following code
                if (null != handler) {
                  Message message = handler.obtainMessage();
                        message.obj = Any data you want to sent to the activity
                        message.what = 1 ; ( Optional )
                        handler.sendMessage(message);
                }

    }

因此,从doInbackground调用sendMessage函数时.您活动中的handleMessage将被触发,然后您应该关闭进度对话框

Thus when sendMessage function is called from doInbackground.. your handleMessage in your activity will get triggered and then you should dismiss the progress dialog

希望这会改善您所面临的性能问题

Hope this will improve the performance issue what you are facing

这篇关于进度对话框异步任务花费比预期更长的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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