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 Handler getMainHandler() {
    if (mainHandler == null) {
        mainHandler = new Handler(Looper.getMainLooper());
    }/*from   w w  w. j  ava 2s  .c om*/
    return mainHandler;
}

From source file:Main.java

/**
 * @return {@code true} if the current thread is the UI thread.
 *//*w  w w . j a va 2  s  . co m*/
public static boolean isOnUiThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:Main.java

public static void showToast(final Context context, final String s) {

    if (Looper.getMainLooper() == Looper.myLooper()) {
        Toast toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
        hideToast();/*www.  j a  v  a  2s .co  m*/
        toast.show();
        sToastRef = new SoftReference<Toast>(toast);
    } else {
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showToast(context, s);
                }
            });
        }
    }
}

From source file:Main.java

private static void showProgressDialog2AutoClose(long betweenTIme) {
    pd.show();//from   w  w  w  . j  ava  2s  .co m
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            printMultiLog("fastRequestShowProgressDialog", "auto close start");
            if (pd != null) {
                printMultiLog("fastRequestShowProgressDialog", "do auto close");
                pd.hide();
            }
            printMultiLog("fastRequestShowProgressDialog", "auto close end");
        }
    }, betweenTIme);
}

From source file:Main.java

public static Handler getHandler() {
    synchronized (handlerLock) {
        if (handler == null) {
            handler = new Handler(Looper.getMainLooper());
        }/*from w  ww. j  a  va2  s .  com*/
    }
    return handler;
}

From source file:Main.java

public static boolean isMainThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:Main.java

/**
 * @return handler associated with the main (UI) thread.
 *//*from  w w w . jav a  2s.  c om*/
public static final Handler getMainThreadHandler() {
    if (sMainThreadHandler == null) {
        sMainThreadHandler = new Handler(Looper.getMainLooper());
    }
    return sMainThreadHandler;
}

From source file:Main.java

/**
 * Returns {@code true} if called on the main thread, {@code false} otherwise.
 *///from ww w  .  j a v  a2s  .c  om
public static boolean isOnMainThread() {
    return Looper.myLooper() == Looper.getMainLooper();
}

From source file:Main.java

private static void throwIfNotCalledOnMainThread() {
    if (!Looper.getMainLooper().isCurrentThread()) {
        throw new IllegalStateException("should be called from the main thread.");
    }//w  w w .  j a  va2s. com
}

From source file:Main.java

public static Handler getHandler() {
    if (handler == null) {
        handler = new Handler(Looper.getMainLooper());
    }//from w  w  w  . j  a  va 2 s.co  m
    return handler;
}