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

/**To display alert dialog 
 * @author wajdihh/*from   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 void postOnUi(Context ctx, Runnable task, long delayMs) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(task, delayMs);//from  www  .  j a  v a  2s. c o m
}

From source file:Main.java

public static void showToast(final Context context, Toast toast, final String s) {
    if (Looper.getMainLooper() == Looper.myLooper()) {
        hideToast();//from  w w w . j  a  va 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

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

From source file:Main.java

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

From source file:Main.java

/**
 * Helper to get the Activity result code.    This must only be called on the main thread.
 *
 * @param activity Activity whose result code is to be obtained.
 * @return Result code of the Activity./*  w  w w.  j  a v  a  2s  . com*/
 */
public static int getActivityResultCode(@NonNull final Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null"); //$NON-NLS-1$
    }

    if (Looper.getMainLooper() != Looper.myLooper()) {
        throw new AssertionError("Must only be called on the main thread");
    }

    /*
     * This is a hack to obtain the Activity result code. There is no official way to check this using the Android
     * testing frameworks, so accessing the internals of the Activity object is the only way. This could
     * break on newer versions of Android.
     */

    try {
        final Field resultCodeField = Activity.class.getDeclaredField("mResultCode"); //$NON-NLS-1$
        resultCodeField.setAccessible(true);
        return ((Integer) resultCodeField.get(activity)).intValue();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void runOnUiThread(Runnable runnable) {
    final Handler UIHandler = new Handler(Looper.getMainLooper());
    UIHandler.post(runnable);//  w  w  w  .j  a v a2 s .  co m
}

From source file:Main.java

/**
 * Helper to get the Activity result Intent.  This must only be called on the main thread.
 *
 * @param activity Activity whose result Intent is to be obtained.
 * @return Result Intent of the Activity.
 *//*from w w  w.  j  a v  a 2 s .  c o m*/
@Nullable
public static Intent getActivityResultData(@NonNull final Activity activity) {
    if (activity == null) {
        throw new IllegalArgumentException("activity cannot be null"); //$NON-NLS-1$
    }

    if (Looper.getMainLooper() != Looper.myLooper()) {
        throw new AssertionError("Must only be called on the main thread");
    }

    /*
     * This is a hack to obtain the Activity result data. There is no official way to check this using the Android
     * testing frameworks, so accessing the internals of the Activity object is the only way. This could
     * break on newer versions of Android.
     */

    try {
        final Field resultIntentField = Activity.class.getDeclaredField("mResultData"); //$NON-NLS-1$
        resultIntentField.setAccessible(true);
        return ((Intent) resultIntentField.get(activity));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.facebook.appevents.internal.AppEventUtility.java

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

From source file:com.tdispatch.passenger.api.ApiHelper.java

public static ApiHelper getInstance(TDApplication app) {
    if (_instance == null) {
        _instance = new ApiHelper();
        _instance.setApplication(app);/*w w w.jav a2 s  .  c om*/
    }

    if (Looper.getMainLooper().equals(Looper.myLooper())) {
        WebnetLog.e("ERROR: instantiated from UI thread!");
    }

    return (_instance);
}