进度对话框异步任务花费比预期更长的时间(Progress dialog async task tak

2019-10-21 16:19发布

我是新来的Android编程。 我开发我使用的是异步任务,其网络爬虫,它正在well.In为了保证用户知情,我使用进度对话框 。 我的问题是,如果我用一个进度对话框我的程序需要更多的时间来执行,当我不会像使用进度对话框,它的执行速度更快。

所做的工作onCreate方法

 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);
    }
}

我缺少的东西吗? 需要帮忙..

感谢和问候,阿比纳夫

Answer 1:

在你的活动

//开始进度对话框..

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);
                }

    }

因此,当SendMessage函数从doInbackground叫..你在你活动的handleMessage将得到触发,那么你就应该驳回进度对话框

希望这会提高你面对什么样的性能问题



Answer 2:

除去super.onPreExecute(); 在onPreExecute()方法和检查。它可以帮助



文章来源: Progress dialog async task taking longer time than expected