1

I know this Question has been asked many times but all answers I got wasn't clear for Me.

I want to implement a File deletion process. I discovered that to do this kind of work it's recommended to run all the process in a separated thread.

For that, I can use AsyncTask, Handler and Thread. AsyncTask seems to be a good solution. However, AsyncTask is designed for small task. (When i try to delete a file whose length is more than 1G, AsyncTask crashes or doesn't execute onPostExecute.

Others thread handlers (Thread, Runnable...) can't manage UI (Update UI or show progress).

My Question is What is the good thread handler for Long tasks and How to manage UI with it.

Glenn
  • 12,741
  • 6
  • 47
  • 48
Charles-Eugene Loubao
  • 1,080
  • 2
  • 12
  • 22
  • Try a background service, from the service start a new thread for the long task and use a broadcast receiver to comnunicate with it and update main ui. – Guy S May 21 '14 at 00:54
  • Your premise is incorrect. AsyncTask was designed, exactly, for the case you describe. You haven't included information about your crash, but it was caused by some other problem. Moving that problem to a Thread is not the solution. – G. Blake Meike Aug 19 '14 at 14:26

4 Answers4

4

First of all, if you need to perform work outside your main thread, but only while the user is interacting with your app, then you should create a new thread, otherwise use a Service.

Now, everything you can do with AsyncTask, you can also do it with a Thread(+ Handler). But the AsyncTask makes the developer job easier, because it is designed to communicate a worker thread with the main thread(caller thread). Now, if you use a Hanlder you can also communicate a worker thread with a caller thread(Note that the caller thread is not necessarily the main thread, it could be communication between two worker threads), if the caller thread is the main thread, you better use AsyncTask.

As far as i know, it goes something like this:

  • Use AsyncTask if you need to run a short task communicating with the UI thread
  • Use a Thread and Handler to run longer tasks that requires communication between the worker thread and the main thread(caller thread)
  • Use Thread, Handler and Looper (or HandlerThread, which is class for starting a thread that already has a Looper) for longer tasks that require communication between the worker thread and the caller thread(not the main thread).
  • Use IntentService for longer task that does not requires user interaction and needs only one worker thread.
ILovemyPoncho
  • 2,762
  • 2
  • 24
  • 37
  • Thanks for your detailed answer. I think Handler and Thread are the best solutions for running Long Tasks and communicate with the UI Thread. I will try it – Charles-Eugene Loubao May 21 '14 at 08:59
  • I think this answer may be a bit misleading. There are very very very few circumstances under which starting your own thread is a good idea. – G. Blake Meike Aug 19 '14 at 14:28
  • Do you mean to use a thread pool instead, and let Android handle the threads? e.g. using the Executor Framework – ILovemyPoncho Aug 19 '14 at 17:12
0

The best for me, is the Asynctask, because the structure is well defined, you can know when is running the thread and when show the result.

Chefes
  • 1,892
  • 18
  • 17
  • Check out this link http://stackoverflow.com/questions/6964011/handler-vs-asynctask-vs-thread – Fareya May 21 '14 at 00:54
0

http://masl.cis.gvsu.edu/2010/04/05/android-code-sample-asynchronous-http-connections/

Good article on using handlers above.

To delete using handler:

Create a runnable that does three things:

Delete file

Obtain msg and send on success

Obtain msg and send on failure

Using some framework to manage threads or using concurrency package, post the runnable and it will do the file Del off main thread. Handler callback will be msg success or msg fail that will process on main thread wherview controller.

Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
0

Check out Needle and you can forget Handlers and AsyncTasks. Needle is an open-source, simple but powerful multithreading library for Android. With it you can say things like:

Needle.onMainThread().execute(new Runnable() {
    @Override
    public void run() {
        // e.g. change one of the views
    }
});

or

Needle.onBackgroundThread().execute(new UiRelatedTask<Integer>() {
    @Override
    protected Integer doWork() {
        int result = 1+2;
        return result;
    }

    @Override
    protected void thenDoUiRelatedWork(Integer result) {
        mSomeTextView.setText("result: " + result);
    }
});
  • very simple API
  • fixed thread pool size
  • customizable thread pool size
  • supports UI interaction ("do work and then use result on UI thread")
  • android 1.5+
  • behaves the same on all platform versions

Check it out on GitHub: https://github.com/ZsoltSafrany/needle

Zsolt Safrany
  • 13,290
  • 6
  • 50
  • 62