Example usage for android.os Handler sendMessage

List of usage examples for android.os Handler sendMessage

Introduction

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

Prototype

public final boolean sendMessage(Message msg) 

Source Link

Document

Pushes a message onto the end of the message queue after all pending messages before the current time.

Usage

From source file:ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java

/**
 * Set the sort mode for the current adapter. Current adapter must be a
 * MediaAdapter. Saves this sort mode to preferences and updates the list
 * associated with the adapter to display the new sort mode.
 *
 * @param mode The sort mode. See {@link MediaAdapter#setSortMode(int)} for
 * details./*from w  w w .j a  v  a 2  s. c o m*/
 */
public void setSortMode(int mode) {
    SortableAdapter adapter = (SortableAdapter) mCurrentAdapter;
    if (mode == adapter.getSortMode())
        return;

    adapter.setSortMode(mode);
    requestRequery(mCurrentAdapter);

    Handler handler = mWorkerHandler;
    handler.sendMessage(handler.obtainMessage(MSG_SAVE_SORT, adapter));
}

From source file:org.jinzora.util.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
    if (urlString == null) {
        if (DBG)//from  ww  w.ja va2  s .  co  m
            Log.d(TAG, "Null image url.");
        synchronized (pendingViews) {
            pendingViews.remove(imageView);
        }
        return;
    }

    synchronized (pendingViews) {
        pendingViews.put(imageView, urlString);
    }

    if (drawableMap.containsKey(urlString)) {
        SoftReference<Drawable> reference = drawableMap.get(urlString);
        Drawable drawable = reference.get();
        if (drawable != null) {
            if (DBG)
                Log.d(TAG, "Image cached in memory.");
            imageView.setImageDrawable(drawable);
            return;
        } else {
            if (DBG)
                Log.d(TAG, "Soft reference cleared.");
            drawableMap.remove(urlString);
        }
    }

    File localFile = getLocalFile(urlString);
    if (localFile != null && localFile.exists()) {
        if (DBG)
            Log.d(TAG, "Image from disk.");
        imageView.setImageDrawable(fetchDrawable(urlString));
        return;
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            synchronized (pendingViews) {
                String latest = pendingViews.get(imageView);
                if (urlString.equals(latest)) {
                    imageView.setImageDrawable((Drawable) message.obj);
                }
            }
        }
    };

    new Thread() {
        @Override
        public void run() {
            Drawable drawable = fetchDrawable(urlString);
            Message message = handler.obtainMessage(1, drawable);
            handler.sendMessage(message);
        }
    }.start();
}

From source file:com.azcltd.android.test.kolesov.DrawableManager.java

public void fetchDrawableOnThread(final String urlString, final ImageView imageView, final boolean isThumbnail,
        final boolean forceUpdate) {

    if (drawableMap.containsKey(urlString) && !forceUpdate) {
        Drawable image = drawableMap.get(urlString);

        SetDrawableToImageView(image, imageView, isThumbnail);
        return;/*from  w  w  w.ja  v  a 2 s .c  o  m*/
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            DrawableWithFlag obj = (DrawableWithFlag) message.obj;
            Drawable image = obj.image;
            boolean flag = obj.isNeedThumbnailed;
            SetDrawableToImageView(image, imageView, flag);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            //setPriority(3);
            Drawable drawable = fetchDrawable(urlString, forceUpdate);
            if (drawable != null) {
                Message message = handler.obtainMessage(1, new DrawableWithFlag(drawable, isThumbnail));
                handler.sendMessage(message);
                //                        try{
                //                            //sleep(100);
                //                        }
                //                        catch(Exception ex) {
                //                            int i =0;
                //                        }
            }
        }
    };
    thread.start();
}

From source file:ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java

@Override
public boolean handleMessage(Message message) {
    switch (message.what) {
    case MSG_RUN_QUERY: {
        LibraryAdapter adapter = (LibraryAdapter) message.obj;
        int index = adapter.getMediaType();
        Handler handler = mUiHandler;
        handler.sendMessage(handler.obtainMessage(MSG_COMMIT_QUERY, index, 0, adapter.query()));
        break;// w  w  w  . j  a  v a2s  . c  o m
    }
    case MSG_COMMIT_QUERY: {
        int index = message.arg1;
        mAdapters[index].commitQuery(message.obj);
        restorePosition(index);
        break;
    }
    case MSG_SAVE_SORT: {
        SortableAdapter adapter = (SortableAdapter) message.obj;
        SharedPreferences.Editor editor = PlaybackService.getSettings(mActivity).edit();
        editor.putInt(adapter.getSortSettingsKey(), adapter.getSortMode());
        editor.apply();
        break;
    }
    case MSG_REQUEST_REQUERY:
        requestRequery((LibraryAdapter) message.obj);
        break;
    default:
        return false;
    }

    return true;
}

From source file:com.scanvine.android.util.SVDownloadManager.java

private void fetchUrlOnThread(final Context context, final String urlString, final Handler handler,
        final boolean isImage) {
    if (urlString == null || urlString.trim().length() == 0)
        return;/*from   w w w .j  a  va 2  s . c o m*/

    Thread thread = new Thread() {
        @Override
        public void run() {
            boolean success = false;
            File localFile = cacheFor(context, urlString);
            if (localFile.exists())
                success = true;
            else {
                Log.i("" + this, "Fetching: " + urlString);
                success = isImage ? fetchDrawable(context, urlString, localFile)
                        : fetchFile(context, urlString, localFile);
            }
            if (success) {
                Message message = handler.obtainMessage(1, localFile);
                handler.sendMessage(message);
            }
        }
    };
    thread.start();
}

From source file:net.sf.golly.HelpActivity.java

private String DownloadFile(String urlstring, String filepath) {
    // we cannot do network connections on main thread, so we do the
    // download on a new thread, but we have to wait for it to finish
    final Handler handler = new LooperInterrupter();

    cancelled = false;/*  w  w w . j  av  a 2  s. c o  m*/
    progbar.setProgress(0);
    // don't show proglayout immediately
    // proglayout.setVisibility(LinearLayout.VISIBLE);

    dresult = "";
    final String durl = urlstring;
    final String dfile = filepath;
    Thread download_thread = new Thread(new Runnable() {
        public void run() {
            dresult = downloadURL(durl, dfile);
            handler.sendMessage(handler.obtainMessage());
        }
    });

    download_thread.setPriority(Thread.MAX_PRIORITY);
    download_thread.start();

    // wait for thread to finish
    try {
        Looper.loop();
    } catch (RuntimeException re) {
    }

    proglayout.setVisibility(LinearLayout.INVISIBLE);

    if (dresult.length() > 0 && !cancelled) {
        Toast.makeText(this, "Download failed! " + dresult, Toast.LENGTH_SHORT).show();
    }
    return dresult;
}

From source file:ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java

/**
 * Schedule a query to be run for the given adapter on the worker thread.
 *
 * @param adapter The adapter to run the query for.
 *///  w w w .  j av a 2 s . c  om
private void postRunQuery(LibraryAdapter adapter) {
    mRequeryNeeded[adapter.getMediaType()] = false;
    Handler handler = mWorkerHandler;
    handler.removeMessages(MSG_RUN_QUERY, adapter);
    handler.sendMessage(handler.obtainMessage(MSG_RUN_QUERY, adapter));
}

From source file:net.evecom.androidecssp.activity.pub.imagescan.NativeImageLoader.java

/**
 * mPointImageViewImageViewBitmap//w w w  .j a v a 2 s .  c om
 * loadNativeImage(final String path, final NativeImageCallBack
 * mCallBack)
 * 
 * @param path
 * @param mPoint
 * @param mCallBack
 * @return
 */
public Bitmap loadNativeImage(final String path, final Point mPoint, final NativeImageCallBack mCallBack) {
    // Bitmap
    Bitmap bitmap = getBitmapFromMemCache(path);

    final Handler mHander = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mCallBack.onImageLoader((Bitmap) msg.obj, path);
        }

    };

    // BitmapBitmapmMemoryCache
    if (bitmap == null) {
        mImageThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                // 
                Bitmap mBitmap = decodeThumbBitmapForFile(path, mPoint == null ? 0 : mPoint.x,
                        mPoint == null ? 0 : mPoint.y);
                Message msg = mHander.obtainMessage();
                msg.obj = mBitmap;
                mHander.sendMessage(msg);

                // 
                addBitmapToMemoryCache(path, mBitmap);
            }
        });
    }
    return bitmap;

}

From source file:ee.ioc.phon.android.speak.RecognizerIntentActivity.java

private void handleResultError(Handler handler, int resultCode, String type, Exception e) {
    if (e != null) {
        Log.e(LOG_TAG, "Exception: " + type + ": " + e.getMessage());
    }//from   www.ja  v a2  s .co  m
    handler.sendMessage(createMessage(MSG_RESULT_ERROR, mErrorMessages.get(resultCode)));
}

From source file:com.dv.BitMap.Native.DvNativeImageLoader.java

/**
 * ?mPoint??ImageView?ImageView???Bitmap
 * ??loadNativeImage(final String path, final NativeImageCallBack mCallBack)?
 *
 * @param path//  w  ww  .  j av a  2 s  . com
 * @param mPoint
 * @param mCallBack
 * @return
 */
public Bitmap loadNativeImage(final String path, final Point mPoint, final DvNativeImageCallBack mCallBack) {

    // ?Bitmap
    Bitmap bitmap = getBitmapFromMemCache(path);
    final Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mCallBack.onImageLoader((Bitmap) msg.obj, path);
        }
    };

    // Bitmap??BitmapmMemoryCache
    if (bitmap == null) {
        mImageThreadPool.execute(new Runnable() {
            @Override
            public void run() {

                // ?
                Bitmap mBitmap = decodeThumbBitmapForFile(path, (mPoint == null) ? 0 : mPoint.x,
                        (mPoint == null) ? 0 : mPoint.y);
                Message msg = mHander.obtainMessage();

                msg.obj = mBitmap;
                mHander.sendMessage(msg);

                // 
                addBitmapToMemoryCache(path, mBitmap);
            }
        });
    }

    return bitmap;
}