3

I created app which searches through the filesystem of android phone, and I would like to update notification in notification bar with information which current file is being searched.

Firstly I wanted to use AsyncTask but then I found that it should not be used with long running operations which searching through filesystem I believe is. From the docs

AsyncTasks should ideally be used for short operations (a few seconds at the most.

Their advice is to use Executor, ThreadPoolExecutor and FutureTask. But no examples is specified. So what exactly should one do to implement this correctly? Can someone give me an example? I don't know if I should have some private class which extends AsyncTask and put run method of outer class to doInBackground method or something..

user3086577
  • 101
  • 1
  • 9

4 Answers4

2

You can use handlers:

The key idea is to create a new Thread that use Handler to update the UI (cause you can't update ui from a Thread that is not the UI thread).

Official docs:

http://developer.android.com/reference/android/os/Handler.html

public class YourClass extends Activity {

    private Button myButton;

    //create an handler
    private final Handler myHandler = new Handler();

    final Runnable updateRunnable = new Runnable() {
        public void run() {
            //call the activity method that updates the UI
            updateUI();
        }
    };


    private void updateUI()
    {
      // ... update the UI      
    }

    private void doSomeHardWork()
    {
        //.... hard work

        //  update the UI using the handler and the runnable
        myHandler.post(updateRunnable);
    }

    private OnClickListener buttonListener = new OnClickListener() {
        public void onClick(View v) {
            new Thread(new Runnable() { 

                    doSomeHardWork();

            }).start();
        }
    };
}
Seraphim's
  • 11,736
  • 17
  • 80
  • 126
  • Okay so two answers for Handlers one for Loaders, is there any difference really? Handler looks easier to implement at least for now I just started reading about them – user3086577 Feb 13 '14 at 17:12
  • 1
    @user3086577 I would suggest loader to pull data into a db, but it's your choice. – Seraphim's Feb 13 '14 at 17:16
  • That is not what I need but thanks for mentioning, might be useful in future – user3086577 Feb 13 '14 at 17:16
  • @user3086577 If you intend to use loaders have a read also at this article, it looks interesting (it's not your specific case because you need to update ui and not pull data into db, anyway...): http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html – Seraphim's Feb 13 '14 at 17:19
1

You can use a thread:

new Thread(new Runnable(){
    @Override
    public void run(){
        //do operation
        //make and show notifications. Notifications are thread safe
    }
}});

You can notify from the worker thread because the notification does not go through your apps process. See here

If you have to post to your app's UI thread, then you can use a handler like so:

//put this in the UI thread
Handler handler = new Handler();
//then within the thread code above
handler.post(new Runnable(){
    @Override
    public void run(){
        //code that you want to put on the UI thread, update views or whatever
    }
}});

Edit: Instead of bothering with Handler, you can use runOnUiThread(...) from your worker thread.

Community
  • 1
  • 1
Reed
  • 13,861
  • 6
  • 61
  • 102
  • Greate answer cause it covers both notification and UI thread update, shame on me I did not find the question on SO you provided. Thank you! – user3086577 Feb 13 '14 at 17:40
  • Also, there might be better ways to do it (such as `Loaders` which others mentioned), but this is how I thread everything and it never causes me any problems so I have not made myself familiar with other ways to do it. – Reed Feb 13 '14 at 17:43
  • Also, you can use `runOnUiThread` instead of the `Handler` to provide the same functionality with less effort. – Reed Feb 15 '14 at 20:39
0

I highly recommend you use Loaders:

http://developer.android.com/guide/components/loaders.html

They were built on top of AsyncTasks to make AsyncTasks easier to manage from within Activities.

danh32
  • 6,094
  • 2
  • 35
  • 39
0

If this is something that should continue to run even when the activity has been closed (a background operation), you should use a Service that you bind to in the Activity. See the Local Service documentation.

dominicoder
  • 5,450
  • 1
  • 17
  • 27