Example usage for android.os Handler post

List of usage examples for android.os Handler post

Introduction

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

Prototype

public final boolean post(Runnable r) 

Source Link

Document

Causes the Runnable r to be added to the message queue.

Usage

From source file:com.manning.androidhacks.hack023.net.NetworkUtilities.java

private static void sendResult(final Boolean result, final Handler handler, final Context context) {
    if (handler == null || context == null) {
        return;// w  w  w  . jav a 2 s . c  om
    }
    handler.post(new Runnable() {
        public void run() {
            ((AuthenticatorActivity) context).onAuthenticationResult(result);
        }
    });
}

From source file:org.microsoftdx.netherclientsample.fragments.LogFragment.java

/**
 * Adds a new log item with the given message.
 *
 * @param message        The message for the log item.
 * @param logMessageType The message type.
 *//*  ww  w  . jav  a2s  .  c  om*/
private static synchronized void addLogItem(String message, LogItem.LogMessageType logMessageType) {
    Timestamp timestamp = new Timestamp(new Date().getTime());
    final LogItem logItem = new LogItem(timestamp, message, logMessageType);

    if (mContext != null && mListAdapter != null) {
        Handler handler = new Handler(mContext.getMainLooper());

        handler.post(new Runnable() {
            @Override
            public void run() {
                mLog.add(0, logItem);

                if (mLog.size() > MAX_NUMBER_OF_LOG_ITEMS) {
                    mLog.remove(mLog.size() - 1); // Remove the last item
                }
                mListAdapter.notifyDataSetChanged();
            }
        });
    }
}

From source file:org.thaliproject.nativetest.app.fragments.LogFragment.java

/**
 * Adds a new log item with the given message.
 * @param message The message for the log item.
 * @param logMessageType The message type.
 *///from ww  w  .  ja v  a 2s. c  om
private static synchronized void addLogItem(String message, LogItem.LogMessageType logMessageType) {
    Timestamp timestamp = new Timestamp(new Date().getTime());
    LogItem logItem = new LogItem(timestamp, message, logMessageType);
    mLog.add(0, logItem);

    if (mLog.size() > MAX_NUMBER_OF_LOG_ITEMS) {
        mLog.remove(mLog.size() - 1); // Remove the last item
    }

    if (mContext != null && mListAdapter != null) {
        Handler handler = new Handler(mContext.getMainLooper());

        handler.post(new Runnable() {
            @Override
            public void run() {
                mListAdapter.notifyDataSetChanged();
            }
        });
    }
}

From source file:com.onesignal.OSUtils.java

static void runOnMainUIThread(Runnable runnable) {
    if (Looper.getMainLooper().getThread() == Thread.currentThread())
        runnable.run();/*  w w w. j  a v  a 2s .c  o m*/
    else {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(runnable);
    }
}

From source file:Main.java

/**
 * Make an asynchronous call in a separate thread, with a callback that's run on the current
 * Android UI thread.//  w  ww  .j  av a2  s. c om
 * @param androidUIHandler  the Handler from the current Android context
 * @param call a {@link Runnable} to run in the thread.
 * @param callback a {@link Runnable} to run in the Android UI thread when the call above returns
 */
public static void runAsynchronously(final Handler androidUIHandler, final Runnable call,
        final Runnable callback) {
    Runnable runnable = new Runnable() {
        public void run() {
            call.run();
            if (callback != null) {
                androidUIHandler.post(new Runnable() {
                    public void run() {
                        callback.run();
                    }
                });
            }
        }
    };
    Thread thread = new Thread(runnable);
    thread.start();
}

From source file:com.kaliturin.blacklist.utils.Utils.java

/**
 * Makes and shows threadsafe toast//from   ww w.jav  a2s.  c o  m
 */
public static void showToast(final Context context, final String message, final int duration) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(context.getApplicationContext(), message, duration).show();
        }
    });
}

From source file:org.liberty.android.fantastischmemo.utils.AMGUIUtility.java

public static void doProgressTask(final Activity activity, final String progressTitle,
        final String progressMessage, final ProgressTask task) {
    final ProgressDialog mProgressDialog = ProgressDialog.show(activity, progressTitle, progressMessage, true);
    final Handler handler = new Handler();
    new Thread() {
        public void run() {
            try {
                task.doHeavyTask();//  w w w . ja  v  a 2s. com
                handler.post(new Runnable() {
                    public void run() {
                        task.doUITask();
                        mProgressDialog.dismiss();
                    }
                });
            } catch (final Exception e) {
                handler.post(new Runnable() {
                    public void run() {
                        mProgressDialog.dismiss();
                        displayException(activity, activity.getString(R.string.exception_text),
                                activity.getString(R.string.exception_message), e);
                        Log.e(TAG, "Error running progress task", e);
                    }
                });
            }
        }
    }.start();
}

From source file:com.scoreflex.ScoreflexGcmClient.java

private static void registerInBackground(final String senderId, final Context activity) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        public void run() {
            new AsyncTask<Object, Object, Object>() {
                @Override/*from   w w w .j  a va 2s . c o  m*/
                protected Object doInBackground(Object... arg0) {
                    String msg = "";
                    try {
                        String regid = ScoreflexGcmWrapper.register(Scoreflex.getApplicationContext(),
                                senderId);
                        if (regid == null) {
                            return null;
                        }
                        msg = "Device registered, registration ID=" + regid;
                        storeRegistrationId(regid, activity);
                        storeRegistrationIdToScoreflex(regid);
                    } catch (IOException ex) {
                        msg = "Error :" + ex.getMessage();
                    }
                    return msg;
                }

                @Override
                protected void onPostExecute(Object msg) {

                }
            }.execute(null, null, null);
        }
    });
}

From source file:net.sourceforge.kalimbaradio.androidapp.util.NotificationUtil.java

public static void hideNotification(final DownloadServiceImpl downloadService, Handler handler) {

    // Remove notification and remove the service from the foreground
    handler.post(new Runnable() {
        @Override//from   w ww .  j  a  v  a  2s .  c  o  m
        public void run() {
            downloadService.stopForeground(true);
        }
    });
}

From source file:com.zoterodroid.client.NetworkUtilities.java

/**
 * Sends the authentication response from server back to the caller main UI
 * thread through its handler./*from  w  w  w .ja v  a2  s  .  c  om*/
 * 
 * @param result The boolean holding authentication result
 * @param handler The main UI thread's handler instance.
 * @param context The caller Activity's context.
 */
private static void sendResult(final LoginResult result, final Handler handler, final Context context) {
    if (handler == null || context == null) {
        return;
    }

    handler.post(new Runnable() {
        public void run() {
            ((AuthenticatorActivity) context).onAuthenticationResult(result);
        }
    });
}