当前位置: 代码迷 >> 综合 >> AsyncTask 使用心得
  详细解决方案

AsyncTask 使用心得

热度:43   发布时间:2024-01-12 10:58:24.0

之前一直使用Handler,最近在试着使用AsyncTask 时由于几个细节没注意老是不顺利。


1、doInBackground是用来执行费时的操作,如下载……,但不能在doInBackground中更新UI界面,如果想要更新UI界面,需要在onPostExecute中执行

2、doInBackground方法和onPostExecute的参数必须对应,这两个参数在AsyncTask声明的泛型参数列表中指定,第一个为doInBackground接受的参数,第二个为显示进度的参数,第第三个为doInBackground返回和onPostExecute传入的参数。

如果参数不对应,如定义AsyncTask时是class LoadDataTask extends AsyncTask<Object, Object, Object>,但protected void onPostExecute(String result),则执行doInBackground后是不会执行onPostExecute的,只有把onPostExecute改成protected void onPostExecute(Object result)才会在执行doInBackground后执行onPostExecute,这样才能实现在onPostExecute在更新UI界面