【问题标题】:Android: Asynctask's doInBackground method is called after a long delayAndroid:长时间延迟后调用Asynctask的doInBackground方法
【发布时间】:2013-01-13 22:47:36
【问题描述】:

我一直在尝试从 url 下载视频,我已经在 asynctask 的 doInBackground() 中实现了我的下载方法,但是 doInBackground 方法需要很长时间才能被调用(5-10 分钟),我是使用另一个异步任务在我被指示下载视频活动的活动中下载图像并且它工作正常。我的 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);
        }
    }

【问题讨论】:

  • 你能发布你的代码吗?一旦 onPreExecute 完成(并且它存在),doInBackground 就会被调用,因此您调用它的方式或 onPreExecute 肯定有问题。
  • 可能他正在使用延迟的帖子,10分钟后不可能运行
  • @D_Steve595 我的 onPreExecute 方法正在按时调用,这是我在调试时发现的,但在那之后 doInBackground 需要将近 5-7 分钟才能启动。
  • @Lumma 没有使用延迟的帖子。
  • 请记住,AsyncTask 调用可能会被序列化,具体取决于您的项目设置和运行的 Android 版本:commonsware.com/blog/2012/04/20/…

标签: android android-asynctask


【解决方案1】:

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

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

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

【讨论】:

  • 是的,使用 executeOnExecutor。谢谢
  • 请注意,使用 executeOnExecutor 仍然会给你很多责任,所以不要创建太多任务(它们对一次有多少个任务有限制)。加上线程越多,应用程序就会变得越慢。
【解决方案2】:

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

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

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

【讨论】:

  • 非常感谢!为我节省了 50 秒的服务呼叫延迟
【解决方案3】:

尽管并非每次都发生,但我也面临同样的问题。

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

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    相关资源
    最近更新 更多