Example usage for android.content Context getMainLooper

List of usage examples for android.content Context getMainLooper

Introduction

In this page you can find the example usage for android.content Context getMainLooper.

Prototype

public abstract Looper getMainLooper();

Source Link

Document

Return the Looper for the main thread of the current process.

Usage

From source file:Main.java

/**
 * Run a some task on the UI thread./*from  w w  w .  j a  va2s  .c  o m*/
 *
 * @param context The context
 * @param r       The runnable object that will be ran on the UI thread.
 */
public static void runOnUiThread(Context context, Runnable r) {
    Handler handler = new Handler(context.getMainLooper());
    handler.post(r);
}

From source file:Main.java

/**
 * <p>Executes a given runnable on the main thread.</p>
 *
 * @param context  An activity context./*from w  ww.j  ava2  s . c om*/
 * @param runnable A runnable to execute on the main thread.
 */
static void runOnMainThread(Context context, Runnable runnable) {
    Handler handler = new Handler(context.getMainLooper());
    handler.post(runnable);
}

From source file:Main.java

private static void a(Context context) {
    Looper looper = Looper.myLooper();//  w w w.j  a va  2 s  .c o  m
    if (looper != null && looper == context.getMainLooper()) {
        throw new IllegalStateException("calling this from your main thread can lead to deadlock");
    } else {
        return;
    }
}

From source file:Main.java

private static Handler sharedHandler(Context context) {
    if (mHandler == null) {
        mHandler = new Handler(context.getMainLooper());
    }/*from  w w w  .ja va2 s.  c  o m*/
    return mHandler;
}

From source file:Main.java

/**
 * Long toast message/*from   www .j  a  v  a  2s.c o  m*/
 * (Predefined in AOS to 3500 ms = 3.5 sec)
 *
 * @param context Application Context
 * @param msg     Message to send
 */
public static void msgLong(final Context context, final String msg) {
    if (context != null && msg != null) {
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg.trim(), Toast.LENGTH_LONG).show();
            }
        });
    }
}

From source file:Main.java

/**
 * Short toast message/*from  w  w  w . ja  v a  2  s  .  c o  m*/
 * (Predefined in AOS to 2000 ms = 2 sec)
 *
 * @param context Application Context
 * @param msg     Message to send
 */
public static void msgShort(final Context context, final String msg) {
    if (context != null && msg != null) {
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg.trim(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

From source file:org.apps8os.logger.android.manager.LoggerWrapper.java

public static void init(final Context context) {
    new Handler(context.getMainLooper()).postDelayed(new Runnable() {
        @Override/*from w  ww.j  a v  a2 s .  c  o  m*/
        public void run() {
            sendLocalNotificationBroadcast(context, true);
        }
    }, 100L);

    if (mActionEventDatabase == null) {
        mActionEventDatabase = new ActionEventDatabase(context.getApplicationContext());
    }
}

From source file:at.jclehner.rxdroid.RxDroid.java

public static void setContext(Context context) {
    sContextRef = new WeakReference<Context>(context);
    sHandler = new Handler(context.getMainLooper());
}

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

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

From source file:de.azapps.mirakel.reminders.ReminderAlarm.java

public static void init(final Context ctx) {
    observer = of(new MirakelContentObserver(new Handler(ctx.getMainLooper()), ctx, Task.URI,
            new MirakelContentObserver.ObserverCallBack() {
                @Override/*from   ww  w.  j  a v a 2  s  .  co  m*/
                public void handleChange() {
                    updateAlarms(ctx);
                }

                @Override
                public void handleChange(final long id) {
                    final Optional<Task> t = Task.get(id);
                    final Calendar c = new GregorianCalendar();
                    if (t.isPresent()) {
                        final Task task = t.get();
                        if (task.getReminder().isPresent() && c.after(task.getReminder().get())) {
                            updateAlarm(ctx, t.get());
                        } else {
                            cancelAlarm(ctx, t.get());
                        }
                    }

                }
            }));
    updateAlarms(ctx);
}