Asynctask没有完成

时间:2016-10-13 13:49:57

标签: android android-activity android-asynctask

我有一个asynctask,我用它做一些计算,然后将这些值写入文件。每隔一段时间我就会称之为Asynctask。有时doInBackground()函数没有完成。不确定它是否陷入循环或某事。对Asynctask的任何进一步调用都不起作用。 onpostexecute()函数也不会被调用。为什么会这样?

我像这样称呼Asyntask:

     MyAsyntask task = new MyAsyntask();
                        task.execute();




 @Override
        protected Boolean doInBackground(String... params) {

         short sample=0;
            try {

                int sampleCount = 200;

                short[] data = new short[sampleCount];

                int o = 0;
                int index = 0;
                while (sampleCount > 0) {

                    sample= (sample+1)/(2)//


                    data[o] = sample ;
                   o++;
                    --sampleCount;

                }

                File f = new File(Environment.getExternalStorageDirectory(), "TestAPP");
                File file = new File(f, "Test" + ".txt");
                try {
                    DataOutputStream out = null;

                    out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

                    for(int i=0; i< data.length; i++)
                    {

                        if(data[i]>=0)
                        {
                            out.writeShort(1);

                        }else
                        {
                            out.writeShort(0);
                        }




                    }
                    out.close();


                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        } 

0 个答案:

没有答案