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:mp.teardrop.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.
 *///from  w  w w  . j  av  a 2 s. c o  m
private void postRunQuery(LibraryAdapter adapter) {
    mRequeryNeeded[adapter != null ? adapter.getMediaType() : MediaUtils.TYPE_UNIFIED] = false;
    Handler handler = mWorkerHandler;
    handler.removeMessages(MSG_RUN_QUERY, adapter);
    handler.sendMessage(handler.obtainMessage(MSG_RUN_QUERY, adapter));
}

From source file:ch.teamuit.android.soundplusplus.LibraryActivity.java

/**
 * Called when a new page becomes visible.
 *
 * @param position The position of the new page.
 * @param adapter The new visible adapter.
 *//* ww w  .  j a  v a  2 s .c  o  m*/
public void onPageChanged(int position, LibraryAdapter adapter) {
    mCurrentAdapter = adapter;
    mLastActedId = LibraryAdapter.INVALID_ID;
    updateLimiterViews();
    if (adapter != null && (adapter.getLimiter() == null || adapter.getMediaType() == MediaUtils.TYPE_FILE)) {
        // Save current page so it is opened on next startup. Don't save if
        // the page was expanded to, as the expanded page isn't the starting
        // point. This limitation does not affect the files tab as the limiter
        // (the files almost always have a limiter)
        Handler handler = mHandler;
        handler.sendMessage(mHandler.obtainMessage(MSG_SAVE_PAGE, position, 0));
    }
}

From source file:ch.ethz.inf.vs.android.g54.a4.types.Building.java

/**
 * Asynchronously get free rooms based on floor and time constraints
 * A message will be dispatched to the handler informing of the status
 * In case of failure, the exception string is passed through the message key of the bundle
 * @param f Floor constraint// w ww  .  j  a  v  a 2s .co  m
 * @param start time constraint in quarter hours
 * @param end time constraint in quarter hours
 * @param handler Handler that will get the success/failure message with this object
 */
public void getFreeRoomsAsync(final Floor f, final Float start, final Float end, final Handler handler) {
    new Thread() {
        public void run() {
            Message m = handler.obtainMessage();
            try {
                m.obj = getFreeRoom(f, start, end);
                m.what = MessageStatus.SUCCESS.ordinal();
            } catch (Exception e) {
                m.what = MessageStatus.FAILURE.ordinal();
                Bundle b = new Bundle();
                b.putString("message", e.getMessage());
                m.setData(b);
            } finally {
                handler.sendMessage(m);
            }
        }
    }.start();
}

From source file:ch.teamuit.android.soundplusplus.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./*  w ww  . j  a  v a  2  s  .  c  om*/
 */
public void setSortMode(int mode) {
    MediaAdapter adapter = (MediaAdapter) mCurrentAdapter;
    if (mode == adapter.getSortMode())
        return;

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

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

From source file:com.binroot.fatpita.BitmapManager.java

public void fetchBitmapOnThread(final String urlString, final ImageView imageView,
        final ProgressBar indeterminateProgressBar, final Activity act, final boolean saveToHistory) {
    SoftReference<Bitmap> ref = mCache.get(urlString);
    if (ref != null && ref.get() != null) {
        imageView.setImageBitmap(ref.get());
        return;//from  w w  w. j  a  v a2s .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, saveToHistory);
            Message message = handler.obtainMessage(1, bitmap);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

From source file:com.cm.beer.util.ContentManager.java

/**
 * /*from  w  w w. j a  v  a  2  s . co m*/
 * @param urlString
 * @param imageView
 */
public void fetchContentOnThread(final String urlString, final Handler handler) {
    if (mContentCache.containsKey(urlString)) {
        SoftReference<String> softReference = mContentCache.get(urlString);

        if ((softReference == null) || (softReference.get() == null)) {
            mContentCache.remove(urlString);
            if (Logger.isLogEnabled())
                Logger.log("fetchContentOnThread():Soft Reference has been Garbage Collected:" + urlString);
        } else {
            String jsonStr = softReference.get();
            if (Logger.isLogEnabled())
                Logger.log(urlString + "=>" + jsonStr);
            Message message = handler.obtainMessage(1, jsonStr);
            handler.sendMessage(message);
            return;
        }
    }

    Thread thread = new Thread() {
        @Override
        public void run() {
            while (mLockCache.containsKey(urlString)) {
                if (Logger.isLogEnabled())
                    Logger.log("URI download request in progress:" + urlString);
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    Log.e(this.getClass().getName(), e.getMessage());
                }
            }
            String content = fetchContent(urlString);
            Message message = handler.obtainMessage(1, content);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

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

/**
 * Call {@link LibraryPagerAdapter#requestRequery(LibraryAdapter)} on the UI
 * thread./*w  w w  . j av  a2s. co  m*/
 *
 * @param adapter The adapter, passed to requestRequery.
 */
public void postRequestRequery(LibraryAdapter adapter) {
    Handler handler = mUiHandler;
    handler.sendMessage(handler.obtainMessage(MSG_REQUEST_REQUERY, adapter));
}

From source file:ch.teamuit.android.soundplusplus.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;//from   www  . j  a v a  2 s. co m
    }
    case MSG_COMMIT_QUERY: {
        int index = message.arg1;
        mAdapters[index].commitQuery(message.obj);

        // Restore scrolling position if present and valid
        Integer curPos = sLruAdapterPos.popPosition(mAdapters[index]);
        if (curPos != null && curPos < mLists[index].getCount())
            mLists[index].setSelection(curPos);

        break;
    }
    case MSG_SAVE_SORT: {
        MediaAdapter adapter = (MediaAdapter) message.obj;
        SharedPreferences.Editor editor = PlaybackService.getSettings(mActivity).edit();
        editor.putInt(String.format("sort_%d_%d", adapter.getMediaType(), adapter.getLimiterType()),
                adapter.getSortMode());
        editor.apply();
        break;
    }
    case MSG_REQUEST_REQUERY:
        requestRequery((LibraryAdapter) message.obj);
        break;
    default:
        return false;
    }

    return true;
}

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 .  jav  a 2  s  .co m
    handler.sendMessage(message);
}

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

/**
 * Called when a new page becomes visible.
 *
 * @param position The position of the new page.
 * @param adapter The new visible adapter.
 *//*  w  w  w .j  a  v a  2  s .com*/
public void onPageChanged(int position, LibraryAdapter adapter) {
    mCurrentAdapter = adapter;
    updateLimiterViews();
    if (adapter != null && (adapter.getLimiter() == null || adapter.getMediaType() == MediaUtils.TYPE_FILE)) {
        // Save current page so it is opened on next startup. Don't save if
        // the page was expanded to, as the expanded page isn't the starting
        // point. This limitation does not affect the files tab as the limiter
        // (the files almost always have a limiter)
        Handler handler = mHandler;
        handler.sendMessage(mHandler.obtainMessage(MSG_SAVE_PAGE, position, 0));
    }
}