Android:Asynctask的doInBackground方法在长时间延迟后调用

时间:2013-01-13 22:47:36

标签: android android-asynctask

我一直在尝试从网址下载视频,我已经在asynctask的doInBackground()中实现了我的下载方法,但是doInBackground方法花了很多时间来调用(5-10分钟),我是使用另一个asyntask下载活动中的图像,我从该活动下载视频活动及其工作正常。我的onPreExecute方法正在按时调用,但之后doInBackground需要大约5-7分钟才能启动。我将非常感谢您提供的任何帮助。 这是mycode

btnDownloadLQ.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {
                try
                {
                    new DownloadVideoTask().execute(videoURL);

                }
                catch(Exception e)
                {
                    Log.e("Vidit_TAG","I got an error",e);
                }
            }
        });

private class DownloadVideoTask extends AsyncTask<String, String, String> 
    {

        @SuppressWarnings("deprecation")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        protected String doInBackground(String... urls) 
        {
            int i=0;
            try
            {
                URL url = new URL (urls[0]);
                InputStream input = url.openStream();

                try {
                    //The sdcard directory e.g. '/sdcard' can be used directly, or 
                    //more safely abstracted with getExternalStorageDirectory()
                    String root = Environment.getExternalStorageDirectory().toString();
                    File storagePath = new File(root + "/vidit");    
                    storagePath.mkdirs();
                    OutputStream output = new FileOutputStream (new File(storagePath,title+".mp4"));
                    try 
                    {
                        byte[] buffer = new byte[1024];
                        int bytesRead = 0;
                        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) 
                        {
                            output.write(buffer, 0, bytesRead);
                        }
                    }
                    catch(Exception e)
                    {   
                        Log.e("Vidit_TAG","I got an error",e);
                    }
                    finally 
                    {
                        output.close();
                    }
                }
                catch(Exception e)
                {
                    Log.e("Vidit_TAG","I got an error",e);
                }
                finally 
                {
                    input.close();
                    //tvTitle.setText("Completed");
                }

            }
            catch(Exception e)
            {
                Log.e("Vidit_TAG","I got an error",e);
            }

            return null;
        }


        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(String unused) 
        {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            alertbox(title);
        }
    }

3 个答案:

答案 0 :(得分:13)

确保没有其他asyncTasks正在运行,如果需要,可以取消它们。

在大多数Android版本上,asyncTask在单个后台线程上运行,并且应该只运行小任务。

如果任务可能花费太长时间(或者有多个任务),请考虑取消它们或使用替代方法(例如使用如on the API所述的executeOnExecutor)。

答案 1 :(得分:1)

迟到的答案,但肯定有帮助

如果您使用的是最低API级别&gt; = 11,请尝试使用

 //new YourAsyncTask().execute(); -- replace this with following line
 new YourAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); //use this

答案 2 :(得分:0)

尽管每次都没有发生,但我面临同样的问题。

您可以使用传统的Thread作为替代方案并自行处理UI更改

相关问题