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

Y0g*_*pta 11 android android-asynctask

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

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

and*_*per 13

通过在需要时取消它们,确保没有其他asyncTasks正在运行.

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

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


Ran*_*mar 5

迟到的回答但肯定有帮助

如果您使用的最低 API 级别 >=11 试试这个

 //new YourAsyncTask().execute(); -- replace this with following line
 new YourAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); //use this
Run Code Online (Sandbox Code Playgroud)