简体   繁体   中英

What to use instead of AsyncTask to update UI for long running operations

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..

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();
        }
    };
}

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM