Example usage for android.os Looper getMainLooper

List of usage examples for android.os Looper getMainLooper

Introduction

In this page you can find the example usage for android.os Looper getMainLooper.

Prototype

public static Looper getMainLooper() 

Source Link

Document

Returns the application's main looper, which lives in the main thread of the application.

Usage

From source file:Main.java

public static void showToast(Context context, String message) {
    new Handler(Looper.getMainLooper()).post(() -> {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    });//  w w w .  jav  a2s.  c  om
}

From source file:Main.java

public static boolean onMainThread() {
    return Looper.myLooper() == Looper.getMainLooper();
}

From source file:Main.java

/**
 * Execute a runnable on the main thread
 *
 * @param action The code to run on the main thread.
 *///from   www. j  a v  a  2s  .  c  o m
public static void runOnMain(Runnable action) {
    runOnLooper(Looper.getMainLooper(), action);
}

From source file:Main.java

public static void showKeyboard(final View view) {
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

        @Override/*ww  w.j a v a2  s . com*/
        public void run() {
            setKeyboardVisible(view, true);
        }
    }, 200);
}

From source file:Main.java

public static void runOnUI(Runnable runnable) {
    if (handler == null) {
        handler = new Handler(Looper.getMainLooper());
    }/*w  w w .  java2s  . co m*/

    handler.post(runnable);
}

From source file:Main.java

/**
 * Returns <tt>true</tt> if current <tt>Thread</tt> is UI thread.
 * @return <tt>true</tt> if current <tt>Thread</tt> is UI thread.
 *///from  w w w.ja  v a 2  s .c  om
public static boolean isUIThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:Main.java

public static void postOnUi(Context ctx, Runnable task) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(task);//from w w w  .j  a v a 2 s .co m
}

From source file:Main.java

/**
 * Execute the given {@link Runnable} on the ui thread.
 *
 * @param runnable The runnable to execute.
 *///from w  ww.j  a  va  2  s.  co m
public static void runOnUiThread(Runnable runnable) {
    Thread uiThread = Looper.getMainLooper().getThread();
    if (Thread.currentThread() != uiThread)
        new Handler(Looper.getMainLooper()).post(runnable);
    else
        runnable.run();
}

From source file:Main.java

/**
 * @return true if the current thread is the UI thread, false otherwise.
 *///from   w ww.j  a  v a 2  s .c  o m
public static boolean currentThreadIsUiThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:Main.java

/**
 * Run the Runnable "delayed" by using an AsyncTask to first require a new
 * thread and only then, in onPostExecute, run the Runnable on the UI thread.
 * @param runnable Runnable to run on UI thread.
 *///from   ww  w .  j  a va  2  s. c o  m
public static void runUiDelayed(final Runnable runnable) {
    (new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... arg0) {
            return null;
        }

        /**
         * Always invoked on UI thread.
         */
        @Override
        protected void onPostExecute(Void result) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(runnable);
        }
    }).execute();
}