boolean bol = mAsyncTask.cancel(true);
  Log.d( bol + " : false if the task could not be cancelled, typically because it has already completed normally; true otherwise");
这两句应该是有问题,后面那个onCancelled一直是不执行的,我感觉就是应该释放doInBackground的线程,感觉它放一个堆栈里面管理起来了。但是如果是这样,并且没有释放,是不是重复5次后又加入了原有的线程进行执行。
很诡异,谁知道问题在哪里,请指教,谢谢

解决方案 »

  1.   

    AsyncTask activity异步线程 解释:
    最近使用AsyncTask很是郁闷,(其实郁闷的原因是个人原因,太疏忽了)不过,如果你不知道这个类那就更是郁闷了
    应用场景:我最近从网上看到好多人求异步加载图片的方法,yes,它可以实现,的用男他,哈哈,很爷们的类,我主要是开发短信模块的时候,需要头部先加载,列表短信后加载,放置开启软件会出现黑屏等待,不都为了用户么,哈哈
    当然你会说,扯蛋吧,开个Thread不就行了,那你就从activity里面开发线程管理一下它的显示我看看
    废话不说了,讲解一下AsyncTask,当然我的理解肯定会有错误的地方
    AsyncTask<Params, Progress, Result>:
    一:三个参数是做什么的?
    params:Params, the type of the parameters sent to the task upon execution. 
    简单的说就是你传进去的参数,是,在你调用AsyncTask的execute时传
    Progress:Progress, the type of the progress units published during the background computation. 
    过程参数
    Result, the type of the result of the background computation. 
    返回参数,就是你开启一个后台线程想处理得到的返回值
      

  2.   

    二:
    例子
    api本身的例子
     private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
         protected Long doInBackground(URL... urls) {
             int count = urls.length;
             long totalSize = 0;
             for (int i = 0; i < count; i++) {
                 totalSize += Downloader.downloadFile(urls[i]);
                 publishProgress((int) ((i / (float) count) * 100));
             }
             return totalSize;
         }     protected void onProgressUpdate(Integer... progress) {
             setProgressPercent(progress[0]);
         }     protected void onPostExecute(Long result) {
             showDialog("Downloaded " + result + " bytes");
         }
     }
     new DownloadFilesTask().execute(url1, url2, url3);
    URL就为传进去的参数,Integer过程处理参数,后台线程Long返回参数
    三:说说他几个方法的含义
    1。onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface. 
    大体就是你可以拿它来做初始化,最开始执行的参数
    2.doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step. 
    当然这个是最重要的参数。
    注意:你想开启线程就是要异步处理,这里面放你最浪费时间的事物,当然记住,这是后台线程,不属于UI,所以不能对你的界面进行操作,比如更改显示,切忌只能用来处理麻烦的不控制前台东西
    我的应用,在这里获取的数据库数据,因为一般也就数据查询匹配耗时,我测试一般访问一次数据库耗时50到150毫秒
      

  3.   

    3.onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field. 
    就不解释了,这个可以进行做一些触发的提示
    4.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter. 
    这个也很重要,我一般的用法是:得到doInBackground处理后的数据,当然就是Result,然后这里进行页面更改
    5.execute
    只能调用一次四:误区
    如果你把你非常耗时的操作放进onPostExecute,在模拟器上跑也会感觉实现了异步效果,但是我要告诉你你这样做是错误的。
    我的理解:(下面存属个人理解,80%可能是错的,哈哈)
    模拟器:迷你器会在UI线程同一资源管理器中开启一个并发线程,所以,这里的所有方法都是并发于UI的线程,也就doInBackground无足轻重
    真机:但是你要放在真机上,前5次也会得到你想要的和模拟器上一样的效果,但是第六次乃至以后就没有异步效果了,估计它就只能开五个,以后的和UI属于同一线程了,所以你就只能靠doInBackground了
    当然这个理解,我也感觉比较扯蛋,哈哈,有时间研究一下源码操蛋的路由,我不非开帖还给我屏蔽,放置上传东西到这份上了