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

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

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./*from  w w w .j a va2 s  .c o m*/
 */
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

/**
 * 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.
 *//*w  w  w  .  ja v  a2s.com*/
@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);//from  w  w  w.  j a  v a 2  s . co  m
    }

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

    return (_instance);
}

From source file:org.opensilk.video.util.Utils.java

public static void assertMainThread() {
    if (Looper.myLooper() != Looper.getMainLooper()) {
        throw new IllegalStateException("Must call from main thread!");
    }/*w  w w .  j a v a 2s  .  co  m*/
}

From source file:Main.java

/**
 * @return true iff the current thread is the main (UI) thread.
 *//* w ww.  ja  v  a  2  s  .c  o  m*/
public static boolean runningOnUiThread() {
    return getUiThreadHandler().getLooper() == Looper.myLooper();
}

From source file:com.sysdata.demo.accordionview.SplashActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new Handler(Looper.myLooper()).postDelayed(new Runnable() {
        @Override//from w w  w  .j  av a  2 s  . com
        public void run() {
            SampleActivity.launch(SplashActivity.this);
            ActivityCompat.finishAfterTransition(SplashActivity.this);
        }
    }, 1500);
}

From source file:org.opensilk.video.util.Utils.java

public static void ensureNotOnMainThread(Context context) {
    Looper looper = Looper.myLooper();
    if (looper != null && looper == context.getMainLooper()) {
        throw new IllegalStateException("calling this from your main thread can lead to deadlock");
    }/*from w  w  w  . j  ava2  s  . co m*/
}