1

I have some asynctasks in my application that do network functions (download/upload files,http requests) in the background.While these actions are in progress I use ProgressDialog in order to display messages about the progress of the task. However some tasks may require more time to complete (for example downloading a file on a slow network) and this is something unwanted in the application.

How can I set a parameter to control the duration of each asynctask? I want the asynctask to complete at some point regardless of the completion of the job in the task. I have to make the asynctask call the onPostExecute method.

I read about this http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29

This was not very helpful because the UI would freeze and there was no actual control of the duration of the asynctask

This is a part of my code

public void downloadFiles(String address) {
        String mainUrl =address;

        //// I overrride the onPostExecute to get
        /// results and call another asynctask
        new Downloader(this){ //<--asynctask
            @Override
            protected void onPostExecute(String result){
                super.onPostExecute(result);
                TestResults=result;

            //another method called that creates another asynctask
             uploadFiles(mainUrl);

            }
        }.execute(mainUrl);


    }

I also tried to use a Handler like this But it didn't work either.

Is there a way to make the asynctask return results (which means to make asynctask call onPostExecute method) after a period of time ?

Using a while loop in the doInBackground method of asnctask is not the solution. I guess I need a timer from the mainUI to make the asynctask return results.

PS I have my application using fragments, that is why I call new Downloader(this) to pass the gui from the fragment.

Just tried this:

public void downloadFiles(String address) {
        String mainUrl =address;


        final Downloader tempObject =new Downloader(this){
            @Override
            protected void onPostExecute(String result){
                super.onPostExecute(result);
                downloadResults=result;
                }
        };
        try {
            tempObject.execute(mainUrl).get(3000L, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

This would make the UI freeze for 3 seconds and then the task would be evoked.... Which is not what I want.

Also tried out this:

Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
          @Override
          public void run() {
              if ( tempObject.getStatus() == Downloader.Status.RUNNING )
                  tempObject.cancel(true);
          }
        }, 5000 );

This would cause the message of onProgressUpdate of asynctask to stop, however the asynctask keeps running....

Any ideas ?

4

1 回答 1

0

Handler 函数的方法需要一些额外的东西才能工作。问题的解决方法就在这里

AsyncTask可能会被取消,但该doInbackground方法仍在运行。实际上任务设置为值“取消”,但doInbackgroung它仍然会运行直到它完成。为了解决这个问题,我们必须在循环中定期检查doInbackground任务是否设置为取消。虽然这不是我想要做的,但这似乎是唯一的解决方案。

doInBackground我们必须检查任务的状态以查看它是否被取消。所以实际上,有人可以在 doInbackground 中使用计时器,让生活更轻松,而无需使用处理程序类。

我发现不能随意终止同步任务的执行令人失望.....如果有人有更好的主意,请告诉我。

于 2013-07-05T00:03:39.610 回答