Example usage for android.os Looper myLooper

List of usage examples for android.os Looper myLooper

Introduction

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

Prototype

public static @Nullable Looper myLooper() 

Source Link

Document

Return the Looper object associated with the current thread.

Usage

From source file:Main.java

/**
 * This class will determine if the current loop being run is on the main thread or not
 * @return boolean, true if on main ui thread, false if not
 *//*from  w  w  w .  jav  a2 s.  com*/
public static boolean isRunningOnMainThread() {
    if (Looper.myLooper() != Looper.getMainLooper()) {
        return false;
    } else {
        return true;
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void removeAllCookiesV21() {
    final CookieManager cookieManager = CookieManager.getInstance();

    Looper looper = Looper.myLooper();
    boolean prepared = false;
    if (looper == null) {
        Looper.prepare();/*w  ww  .  jav a 2s.c o m*/
        prepared = true;
    }

    // requires a looper
    cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
        @Override
        public void onReceiveValue(Boolean value) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    // is synchronous, run in background
                    cookieManager.flush();
                }
            };
            thread.start();
        }
    });

    if (prepared) {
        looper = Looper.myLooper();
        if (looper != null) {
            looper.quit();
        }
    }
}

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();/*from  w ww.ja  v a 2 s .c om*/
        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

/**
 * Returns {@code true} if called on the main thread, {@code false} otherwise.
 *//*from w ww.java 2s.  c o  m*/
public static boolean isOnMainThread() {
    return Looper.myLooper() == Looper.getMainLooper();
}

From source file:Main.java

/**
 * Checks whether the current thread is the same thread that the {@link Handler} is associated
 * with./*from www  . ja  v  a 2s. c  om*/
 * @return true if the current thread is the same thread that the {@link Handler} is associated
 * with; otherwise false.
 */
public static boolean checkThreadAccess(Handler handler) {
    return Looper.myLooper() == handler.getLooper();
}

From source file:Main.java

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

From source file:io.amira.zen.http.ZenHTTP.java

private static AsyncHttpClient client() {
    if (Looper.myLooper() == null) {
        return syncClient;
    }
    return asyncClient;
}

From source file:Main.java

public static void quitLoop() {
    try {//from   w w w. j  a v  a  2 s .c  o m
        Field mQuitAllowed = MessageQueue.class.getDeclaredField("mQuitAllowed");
        mQuitAllowed.setAccessible(true);

        mQuitAllowed.set(Looper.myQueue(), true);

        Looper.myLooper().quit();

        Field mQuiting = MessageQueue.class.getDeclaredField("mQuiting");
        mQuiting.setAccessible(true);

        mQuiting.set(Looper.myQueue(), false);
        mQuitAllowed.set(Looper.myQueue(), false);
    } catch (Exception e) {
        throw new IllegalStateException(
                "Sofia modal runnables are not " + "supported on this version of the Android API because the "
                        + "MessageQueue.mQuitAllowed field could not be found or "
                        + "there was a problem changing it.");
    }
}

From source file:Main.java

/**To display alert dialog 
 * @author wajdihh/*w  ww .j av  a  2  s.  c om*/
 * Display dialog
 * @param pContext
 * @param pTitle
 * @param pMessage
 */
public static void showMessage(Context pContext, String pTitle, String pMessage) {

    if (Looper.getMainLooper() == Looper.myLooper()) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(pContext);
        dialog.setTitle(pTitle);
        dialog.setMessage(pMessage);
        dialog.setNeutralButton("Ok", null);
        dialog.create().show();
    } else
        Log.e("ShowMessageLog", pTitle + " : " + pMessage);
}

From source file:Main.java

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