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:com.amytech.android.library.utils.asynchttp.AsyncHttpClient.java

/**
 * Cancels any pending (or potentially active) requests associated with the
 * passed Context./*  www .  j a  v a 2 s. com*/
 * <p>
 * &nbsp;
 * </p>
 * <b>Note:</b> This will only affect requests which were created with a
 * non-null android Context. This method is intended to be used in the
 * onDestroy method of your android activities to destroy all requests which
 * are no longer required.
 *
 * @param context
 *            the android Context instance associated to the request.
 * @param mayInterruptIfRunning
 *            specifies if active requests should be cancelled along with
 *            pending requests.
 */
public void cancelRequests(final Context context, final boolean mayInterruptIfRunning) {
    if (context == null) {
        Log.e(LOG_TAG, "Passed null Context to cancelRequests");
        return;
    }

    final List<RequestHandle> requestList = requestMap.get(context);
    requestMap.remove(context);

    if (Looper.myLooper() == Looper.getMainLooper()) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                cancelRequests(requestList, mayInterruptIfRunning);
            }
        };
        threadPool.submit(runnable);
    } else {
        cancelRequests(requestList, mayInterruptIfRunning);
    }
}

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

/**
 * Cancels any pending (or potentially active) requests associated with the passed Context.
 * <p>&nbsp;</p> <b>Note:</b> This will only affect requests which were created with a non-null
 * android Context. This method is intended to be used in the onDestroy method of your android
 * activities to destroy all requests which are no longer required.
 *
 * @param context               the android Context instance associated to the request.
 * @param mayInterruptIfRunning specifies if active requests should be cancelled along with
 *                              pending requests.
 */// w  ww. j av  a  2  s  . c  o m
public void cancelRequests(final Context context, final boolean mayInterruptIfRunning) {
    if (context == null) {
        log.e(LOG_TAG, "Passed null Context to cancelRequests");
        return;
    }

    final List<RequestHandle> requestList = requestMap.get(context);
    requestMap.remove(context);

    if (Looper.myLooper() == Looper.getMainLooper()) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                cancelRequests(requestList, mayInterruptIfRunning);
            }
        };
        threadPool.submit(runnable);
    } else {
        cancelRequests(requestList, mayInterruptIfRunning);
    }
}

From source file:android.support.v7.media.MediaRouter.java

/**
 * Ensures that calls into the media router are on the correct thread.
 * It pays to be a little paranoid when global state invariants are at risk.
 *///from w  w w  . j a  v  a  2  s  .c om
static void checkCallingThread() {
    if (Looper.myLooper() != Looper.getMainLooper()) {
        throw new IllegalStateException(
                "The media router service must only be " + "accessed on the application's main thread.");
    }
}

From source file:com.networking.ApiTestActivity.java

public void setMaxAgeCacheControl(View view) {
    AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0")
            .addQueryParameter("limit", "3").setTag(this).setPriority(Priority.LOW)
            .setMaxAgeCacheControl(0, TimeUnit.SECONDS).build().setAnalyticsListener(new AnalyticsListener() {
                @Override/*  w  ww.j  a  v  a2s. 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);
                }
            }).getAsJSONArray(new JSONArrayRequestListener() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, "onResponse array : " + 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.networking.OkHttpResponseTestActivity.java

public void setMaxStaleCacheControl(View view) {
    AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0")
            .addQueryParameter("limit", "3").setTag(this).setPriority(Priority.LOW)
            .setMaxStaleCacheControl(365, TimeUnit.SECONDS).build()
            .setAnalyticsListener(new AnalyticsListener() {
                @Override/*  w  w  w  .  jav a2 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);
                }
            }).getAsOkHttpResponseAndJSONArray(new OkHttpResponseAndJSONArrayRequestListener() {
                @Override
                public void onResponse(Response okHttpResponse, JSONArray 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:com.networking.ApiTestActivity.java

public void setMaxStaleCacheControl(View view) {
    AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY).addPathParameter("pageNumber", "0")
            .addQueryParameter("limit", "3").setTag(this).setPriority(Priority.LOW)
            .setMaxStaleCacheControl(365, TimeUnit.SECONDS).build()
            .setAnalyticsListener(new AnalyticsListener() {
                @Override//from ww  w. j av a2 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);
                }
            }).getAsJSONArray(new JSONArrayRequestListener() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, "onResponse array : " + 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: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  ww.j a v a  2  s  .co m
    } 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//from  w w w.j  a v 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);
                }
            }).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//from w w  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);
                }
            }).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);
                }
            });
}