Example usage for android.os Handler obtainMessage

List of usage examples for android.os Handler obtainMessage

Introduction

In this page you can find the example usage for android.os Handler obtainMessage.

Prototype

public final Message obtainMessage(int what, Object obj) 

Source Link

Document

Same as #obtainMessage() , except that it also sets the what and obj members of the returned Message.

Usage

From source file:com.mytutor.search.ImageLoader.java

public void fetchDrawableOnThread(final String urlString, Drawable drawable) {
    if (drawableMap.containsKey(urlString)) {
        drawable = drawableMap.get(urlString);
    }/*ww w .  j ava  2  s .  c  o m*/

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            final Drawable drawable = (Drawable) message.obj;
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image
            Drawable draw = fetchDrawable(urlString);
            Message message = handler.obtainMessage(1, draw);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.mytutor.search.ImageLoader.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (drawableMap.containsKey(urlString)) {
        Log.d("IMG 1", "Getting Image");
        imageView.setImageDrawable(drawableMap.get(urlString));
    }/*from   www . ja  v a 2  s  .c o m*/

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image
            Drawable drawable = fetchDrawable(urlString);
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.fabbychat.utils.DrawableManager.java

public void fetchDrawableOnThread(final DrawableProducer dp, final ImageView imageView) {

    String key = dp.getKey();/*  w ww . ja  v  a 2  s  . c o  m*/
    if (drawableMap.containsKey(key)) {
        Drawable d = drawableMap.get(key);
        if (d != null) {
            imageView.setImageDrawable(drawableMap.get(key));
        }
        return;
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image
            Drawable drawable = fetchDrawable(dp);
            if (drawable != null) {
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            }
        }
    };
    thread.start();
}

From source file:eu.loopit.f2011.library.BitmapManager.java

public void fetchBitmapOnThread(final String urlString, final ImageView imageView, final Bitmap defaultImage) {
    if (bitmapMap.containsKey(urlString)) {
        imageView.setImageBitmap(bitmapMap.get(urlString));
    }/*  w  w  w  .ja  v  a2 s . co  m*/

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            if (message.obj != null) {
                imageView.setImageBitmap((Bitmap) message.obj);
            }
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            Bitmap bitmap = fetchBitmap(urlString, defaultImage);
            Message message = handler.obtainMessage(1, bitmap);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:it.fdev.utils.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (drawableMap.containsKey(urlString)) {
        imageView.setImageDrawable(drawableMap.get(urlString));
    }/*from  w w  w .  j a  va  2  s.c  o m*/
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };
    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image
            Drawable drawable = fetchDrawable(urlString);
            if (drawable != null) {
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            }
        }
    };
    thread.start();
}

From source file:com.oonusave.coupon.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (drawableMap.containsKey(urlString)) {
        imageView.setImageDrawable(drawableMap.get(urlString));

        return;// w w  w  .  j  a  va  2 s. c  o m
    }

    if (urlString != null && "".equalsIgnoreCase(urlString)) {
        return;
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            //TODO : set imageView to a "pending" image
            Drawable drawable = fetchDrawable(urlString);
            drawableMap.put(urlString, drawable);
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:org.neugierig.proximo.AsyncBackend.java

public void startQuery(final Query query, final APIResultCallback callback) {
    final int MSG_RESULT = 0;
    final int MSG_EXCEPTION = 1;

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_RESULT:
                callback.onAPIResult(msg.obj);
                break;
            case MSG_EXCEPTION:
                callback.onException((Exception) msg.obj);
                break;
            }//from   ww  w  .ja v  a2 s.  c o m
        }
    };

    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                Object result = query.runQuery(mBackend);
                handler.sendMessage(handler.obtainMessage(MSG_RESULT, result));
            } catch (Exception exn) {
                handler.sendMessage(handler.obtainMessage(MSG_EXCEPTION, (Object) exn));
            }
        }
    }, "Network Fetch");
    thread.start();
}

From source file:com.example.linhdq.test.documents.viewing.grid.DocumentGridActivity.java

private void postDocumentThumbnails() {
    Handler handler = mScrollHandler;
    Message message = handler.obtainMessage(MESSAGE_UPDATE_THUMNAILS, DocumentGridActivity.this);
    handler.removeMessages(MESSAGE_UPDATE_THUMNAILS);
    mPendingThumbnailUpdate = true;/* w  ww .  j av a2  s  . c o m*/
    handler.sendMessage(message);
}

From source file:com.andrewshu.android.reddit.threads.BitmapManager.java

public void fetchBitmapOnThread(final String urlString, final ImageView imageView,
        final ProgressBar indeterminateProgressBar, final Activity act) {
    SoftReference<Bitmap> ref = mCache.get(urlString);
    if (ref != null && ref.get() != null) {
        imageView.setImageBitmap(ref.get());
        return;/*  ww  w .j  a  v  a 2 s . c o m*/
    }

    final Runnable progressBarShow = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                imageView.setVisibility(View.GONE);
                indeterminateProgressBar.setVisibility(View.VISIBLE);
            }
        }
    };
    final Runnable progressBarHide = new Runnable() {
        public void run() {
            if (indeterminateProgressBar != null) {
                indeterminateProgressBar.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
            }
        }
    };

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarHide);
            imageView.setImageBitmap((Bitmap) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            if (indeterminateProgressBar != null && act != null)
                act.runOnUiThread(progressBarShow);
            Bitmap bitmap = fetchBitmap(urlString);
            Message message = handler.obtainMessage(1, bitmap);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.temboo.example.YouTubeResultView.java

/**
 * A utility method to retrieve the thumbnail image on a separate thread, and populate
 * the image view with that thumbnail// w w  w  .j  a va  2 s .com
 * @param urlString - the URL of the thumbnail to retrieve
 * @param imageView - the view to populate with the thumbnail
 */
private void fetchDrawableOnThread(final String urlString, final ImageView imageView) {

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageView.setImageDrawable((Drawable) message.obj);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet request = new HttpGet(urlString);
                HttpResponse response = httpClient.execute(request);
                InputStream is = response.getEntity().getContent();
                Drawable drawable = Drawable.createFromStream(is, "src");

                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);
            } catch (Exception e) {
                Toast.makeText(getContext(), "An error occurred retrieving the video thumbnail",
                        Toast.LENGTH_LONG).show();
            }
        }
    };
    thread.start();
}