Build and Release

A continuous learner for experience and life.

Programming for Andorid: Synchronize Actions With Handler

enter image description here

Sometimes, we need to synchronize the actions in a series but not in parallel. For example, we need to download an image then display it on Google Gallery. It seems we cannot display a partial image during downloading. OK, there is another story, probably, we will mention later, for displaying image with a coarse one then detailed the clear picture. Now let’s fix how to download an image then display it, without manual interference.

This program bases on the last one: Programming for Android: Download, Progressbar and FileProvider

Steps:

1. Declare a handler and a message ID, which we need to indicate what action is completed:

1
2
Handler handler;
private static final int DOWNLOAD_COMPLETED = 0;

2. Send out the message, when the first action is completed:

1
2
3
4
                // send message to trigger the following event
                Message msg = Message.obtain();
                msg.what = DOWNLOAD_COMPLETED;
                handler.sendMessage(msg);

3. Handle the message, then trigger the following action(s):

1
2
3
4
5
6
7
8
9
10
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DOWNLOAD_COMPLETED:
                    startDisplay();
                    break;
            }
        }
    };

You can clone the entire source code here [2].

References:

  1. http://stackoverflow.com/questions/4592716/multithreading-question
  2. https://github.com/lifuzu/FileProviderExample

Written with StackEdit.

Comments