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:com.onesignal.OneSignal.java

private static void fireNotificationOpenedHandler(final String message, final JSONObject additionalDataJSON,
        final boolean isActive) {
    if (Looper.getMainLooper().getThread() == Thread.currentThread()) // isUIThread
        notificationOpenedHandler.notificationOpened(message, additionalDataJSON, isActive);
    else {//  w w w  .  ja v a 2  s  . co m
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                notificationOpenedHandler.notificationOpened(message, additionalDataJSON, isActive);
            }
        });
    }
}

From source file:android.support.v7.widget.BaseRecyclerViewInstrumentationTest.java

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

From source file:android.support.v7.widget.BaseRecyclerViewInstrumentationTest.java

public void runTestOnUiThread(Runnable r) throws Throwable {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        r.run();/* w  w w  .  j  a v  a 2s .c  om*/
    } else {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(r);
    }
}

From source file:com.networking.ApiTestActivity.java

public void cleanupDestinationTest(View view) {
    String url = "http://i.imgur.com/m6K1DCQ.jpg";
    AndroidNetworking.download(url, Utils.getRootDirPath(getApplicationContext()), "cleanupDestinationTest.jpg")
            .setPriority(Priority.HIGH).setTag("cleanupDestinationTest").build()
            .setAnalyticsListener(new AnalyticsListener() {
                @Override//w  ww .  j  a v a  2s  .c o  m
                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);
                }
            }).setDownloadProgressListener(new DownloadProgressListener() {
                @Override
                public void onProgress(long bytesDownloaded, long totalBytes) {
                    Log.d(TAG, "bytesDownloaded : " + bytesDownloaded + " totalBytes : " + totalBytes);
                    Log.d(TAG, "setDownloadProgressListener isMainThread : "
                            + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                    if (bytesDownloaded > 50) {
                        AndroidNetworking.cancel("cleanupDestinationTest");
                        Log.d(TAG, "cancel: cleanupDestinationTest");
                    }
                }
            }).startDownload(new DownloadListener() {
                @Override
                public void onDownloadComplete() {
                    Log.d(TAG, "File download Completed");
                    Log.d(TAG, "onDownloadComplete isMainThread : "
                            + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                }

                @Override
                public void onError(ANError error) {
                    if (error.getErrorCode() != 0) {
                        // received ANError from server
                        // error.getErrorCode() - the ANError code from server
                        // error.getErrorBody() - the ANError body from server
                        // error.getErrorDetail() - just an ANError detail
                        Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                        Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                        Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                    } else {
                        // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                        Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                    }
                }
            });
}

From source file:com.networking.OkHttpResponseTestActivity.java

public void disableGzipForCustomRequest(View view) {
    AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER)
            .addBodyParameter("firstname", "Amit").addBodyParameter("lastname", "Shekhar").setTag(this)
            .setOkHttpClient(new OkHttpClient()).setPriority(Priority.LOW).build()
            .setAnalyticsListener(new AnalyticsListener() {
                @Override/* w w  w .  j av  a  2  s  .  co  m*/
                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);
                }
            }).getAsOkHttpResponseAndJSONObject(new OkHttpResponseAndJSONObjectRequestListener() {
                @Override
                public void onResponse(Response okHttpResponse, JSONObject response) {
                    Log.d(TAG, "onResponse object : " + response.toString());
                    Log.d(TAG, "onResponse isMainThread : "
                            + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                    if (okHttpResponse.isSuccessful()) {
                        Log.d(TAG, "onResponse success headers : " + okHttpResponse.headers().toString());
                    } else {
                        Log.d(TAG, "onResponse not success headers : " + okHttpResponse.headers().toString());
                    }
                }

                @Override
                public void onError(ANError anError) {
                    Utils.logError(TAG, anError);
                }
            });
}

From source file:org.cryptsecure.Utility.java

/**
 * Show toast short async from non-UI thread.
 * // w w  w.  ja  va2  s. com
 * @param context
 *            the context
 * @param toastText
 *            the toast text
 */
public static void showToastShortAsync(final Context context, final String toastText) {
    final Handler mUIHandler = new Handler(Looper.getMainLooper());
    mUIHandler.post(new Thread() {
        @Override
        public void run() {
            super.run();
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, toastText, duration);
            toast.show();
        }
    });
}

From source file:org.cryptsecure.Utility.java

/**
 * Show toast async from non-UI thread./*from   w  w w  . j a v a 2 s  . c  o m*/
 * 
 * @param context
 *            the context
 * @param toastText
 *            the toast text
 */
public static void showToastAsync(final Context context, final String toastText) {
    final Handler mUIHandler = new Handler(Looper.getMainLooper());
    mUIHandler.post(new Thread() {
        @Override
        public void run() {
            super.run();
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, toastText, duration);
            toast.show();
        }
    });
}

From source file:com.networking.ApiTestActivity.java

public void disableGzipForCustomRequest(View view) {
    AndroidNetworking.post(ApiEndPoint.BASE_URL + ApiEndPoint.POST_CREATE_AN_USER)
            .addBodyParameter("firstname", "Amit").addBodyParameter("lastname", "Shekhar").setTag(this)
            .setOkHttpClient(new OkHttpClient()).setPriority(Priority.LOW).build()
            .setAnalyticsListener(new AnalyticsListener() {
                @Override//ww  w . j av  a2 s.com
                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);
                }
            }).getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "onResponse object : " + response.toString());
                    Log.d(TAG, "onResponse isMainThread : "
                            + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                }

                @Override
                public void onError(ANError error) {
                    if (error.getErrorCode() != 0) {
                        // received ANError from server
                        // error.getErrorCode() - the ANError code from server
                        // error.getErrorBody() - the ANError body from server
                        // error.getErrorDetail() - just a ANError detail
                        Log.d(TAG, "onError errorCode : " + error.getErrorCode());
                        Log.d(TAG, "onError errorBody : " + error.getErrorBody());
                        Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                    } else {
                        // error.getErrorDetail() : connectionError, parseError, requestCancelledError
                        Log.d(TAG, "onError errorDetail : " + error.getErrorDetail());
                    }
                }
            });
}

From source file:com.digium.respokesdk.RespokeClient.java

/**
 *  A convenience method for posting errors to a ConnectCompletionListener
 *
 *  @param completionListener  The listener to notify
 *  @param errorMessage        The human-readable error message that occurred
 *//*from  w  w  w.  ja  va  2s . c o  m*/
private void postConnectError(final ConnectCompletionListener completionListener, final String errorMessage) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            if (null != completionListener) {
                completionListener.onError(errorMessage);
            }
        }
    });
}

From source file:com.digium.respokesdk.RespokeClient.java

/**
 *  A convenience method for posting errors to a JoinGroupCompletionListener
 *
 *  @param completionListener  The listener to notify
 *  @param errorMessage        The human-readable error message that occurred
 *//*from  w w w. ja v  a 2 s.  c o m*/
private void postJoinGroupMembersError(final JoinGroupCompletionListener completionListener,
        final String errorMessage) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            if (null != completionListener) {
                completionListener.onError(errorMessage);
            }
        }
    });
}