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:eu.geopaparazzi.library.util.Utilities.java

/**
 * Checks if we are on the UI thread./*  w w w  .  j a  v  a  2  s . c o m*/
 *
 * @return <code>true</code> if we are on the UI thread.
 */
public static boolean isInUiThread() {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        // UI Thread
        return true;
    }
    return false;
}

From source file:com.rxsampleapp.RxApiTestActivity.java

public void getAllUsers(View view) {
    RxAndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY)
            .addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").build()
            .setAnalyticsListener(new AnalyticsListener() {
                @Override/*from  ww w  . jav a  2 s.c  om*/
                public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived,
                        boolean isFromCache) {
                    Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
                    Log.d(TAG, " bytesSent : " + bytesSent);
                    Log.d(TAG, " bytesReceived : " + bytesReceived);
                    Log.d(TAG, " isFromCache : " + isFromCache);
                }
            }).getParseObservable(new TypeToken<List<User>>() {
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<User>>() {
                @Override
                public void onCompleted() {
                    Log.d(TAG, "onComplete Detail : getAllUsers completed");
                }

                @Override
                public void onError(Throwable e) {
                    Utils.logError(TAG, e);
                }

                @Override
                public void onNext(List<User> users) {
                    Log.d(TAG, "onResponse isMainThread : "
                            + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                    Log.d(TAG, "userList size : " + users.size());
                    for (User user : users) {
                        Log.d(TAG, "id : " + user.id);
                        Log.d(TAG, "firstname : " + user.firstname);
                        Log.d(TAG, "lastname : " + user.lastname);
                    }
                }
            });
}

From source file:com.gtosoft.libvoyager.net.GTONet.java

/**
 * Creates a new "worker" thread, to which we can post new work to be performed asynchronously from the main (UI) thread. 
 *//*from   w w  w  .  ja va2  s  .  c om*/
public void startWorkerThread() {
    if (mWorkerThread != null)
        return;

    // create a new thread. 
    mWorkerThread = new Thread() {
        public void run() {
            //mWorkerHandler.getLooper();
            Looper.prepare();

            // set the worker to a new handler owned by this thead. 
            mWorkerHandler = new Handler();

            // main loop. just loop, sleeping, waiting for work. 
            while (1 == 1) {
                Looper.loop();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    break; // break out of the while loop, kills the thread. 
                }

            } // end while loop. 
            Looper.myLooper().quit();
        }// end of thread run() 
    };
    mWorkerThread.start();

    // sleep for a second while thread starts... Prevents calling tasks from hitting it before it has a chance to start!
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        return;
    }

}

From source file:cn.com.loopj.android.http.AsyncHttpResponseHandler.java

/**
 * Creates a new AsyncHttpResponseHandler with a user-supplied looper. If
 * the passed looper is null, the looper attached to the current thread will
 * be used.// w  w w  . j  av  a 2s  .c  o m
 *
 * @param looper The looper to work with
 */
public AsyncHttpResponseHandler(Looper looper) {
    this.looper = looper == null ? Looper.myLooper() : looper;

    // Use asynchronous mode by default.
    setUseSynchronousMode(false);

    // Do not use the pool's thread to fire callbacks by default.
    setUsePoolThread(false);
}

From source file:org.peterbaldwin.vlcremote.app.PickServerActivity.java

private PortSweeper createPortSweeper(PortSweeper.Callback callback) {
    return new PortSweeper(mPort, mFile, mWorkers, callback, Looper.myLooper());
}

From source file:com.honeywell.printer.net.autobaln_websocket.WebSocketConnection.java

private void failConnection(WebSocketCloseNotification code, String reason) {
    Log.d(TAG, "fail connection [code = " + code + ", reason = " + reason);

    if (mWebSocketReader != null) {
        mWebSocketReader.quit();/*  www.j ava  2  s.  c  o  m*/

        try {
            mWebSocketReader.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } else {
        Log.d(TAG, "mReader already NULL");
    }

    if (mWebSocketWriter != null) {
        mWebSocketWriter.forward(new WebSocketMessage.Quit());

        try {
            mWebSocketWriter.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } else {
        Log.d(TAG, "mWriter already NULL");
    }

    if (mSocket != null) {
        mSocketThread.getHandler().post(new Runnable() {

            @Override
            public void run() {
                mSocketThread.stopConnection();
            }
        });
    } else {
        Log.d(TAG, "mTransportChannel already NULL");
    }

    mSocketThread.getHandler().post(new Runnable() {

        @Override
        public void run() {
            Looper.myLooper().quit();
        }
    });

    onClose(code, reason);

    Log.d(TAG, "worker threads stopped");
}

From source file:com.seo.support.http.AsyncHttpResponseHandler.java

@Override
public void setUseSynchronousMode(boolean value) {
    // A looper must be prepared before setting asynchronous mode.
    if (!value && Looper.myLooper() == null) {
        value = true;//from   ww w . j a  v a 2s  . co m
        Log.w(LOG_TAG, "Current thread has not called Looper.prepare(). Forcing synchronous mode.");
        Log.w(LOG_TAG, "request uri is " + getRequestURI());
    }

    // If using synchronous mode.
    if (!value && handler == null) {
        // Create a handler on current thread to submit tasks
        handler = new ResponderHandler(this);
    } else if (value && handler != null) {
        // TODO: Consider adding a flag to remove all queued messages.
        handler = null;
    }

    useSynchronousMode = value;
}

From source file:com.loopj.android.http.handler.AsyncHttpResponseHandler.java

@Override
public void setUseSynchronousMode(boolean value) {
    // A looper must be prepared before setting asynchronous mode.
    if (!value && Looper.myLooper() == null) {
        value = true;//from   w ww . j a va2s. co m
        Log.w(LOG_TAG, "Current thread has not called Looper.prepare(). Forcing synchronous mode.");
    }

    // If using synchronous mode.
    if (!value && handler == null) {
        // Create a handler on current thread to submit tasks
        handler = new ResponderHandler(this);
    } else if (value && handler != null) {
        // TODO: Consider adding a flag to remove all queued messages.
        handler = null;
    }

    useSynchronousMode = value;
}

From source file:com.aniruddhfichadia.presentable.PresentableFragment.java

/** A {@link Fragment} equivalent of {@link android.app.Activity#runOnUiThread(Runnable)}. */
protected void runOnUiThread(@NonNull Runnable runnable) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        // This is the main looper/thread, just execute the runnable
        runnable.run();//from ww w  . jav a2  s.c  om
    } else {
        synchronized (uiHandlerLock) {
            // Not the main looper, post event on it
            if (uiHandler == null) {
                uiHandler = new Handler(Looper.getMainLooper());
            }
            uiHandler.post(runnable);
        }
    }
}

From source file:io.jxcore.node.jxcore.java

public static boolean CallJSMethod(String id, Object[] args) {
    if (jxcore.coreThread == null) {
        Log.e(LOGTAG, "JXcore wasn't initialized yet");
        return false;
    }/*www  .  j  a  v  a2s .  co m*/

    if (Looper.myLooper() != coreThread.handler.getLooper()) {
        coreThread.handler.post(new CoreRunnable(id, args) {
            @Override
            public void run() {
                callJSMethod(callback_id_, params_);
            }
        });
    } else {
        callJSMethod(id, args);
    }

    return true;
}