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:com.google.android.gcm.demo.ui.MainActivity.java

static public void showToast(final Context context, final int msgId, final Object... args) {
    Handler handler = new Handler(context.getMainLooper());
    handler.post(new Runnable() {
        public void run() {
            Toast.makeText(context, context.getString(msgId, args), Toast.LENGTH_LONG).show();
        }/* www. j  a  v  a  2  s .  c  o m*/
    });
}

From source file:com.arellomobile.android.push.PushManager.java

public static void sendTags(final Context context, final Map<String, Object> tags,
        final SendPushTagsCallBack callBack) {
    Handler handler = new Handler(context.getMainLooper());
    handler.post(new Runnable() {
        @SuppressWarnings("unchecked")
        public void run() {
            new SendPushTagsAsyncTask(context, callBack).execute(tags);
        }/*w  w  w.j  a  v  a2s. co  m*/
    });
}

From source file:org.ttrssreader.utils.Utils.java

/**
 * Allos to send a toast from a background thread
 *
 * @param context the context, eg. MyApplication.context()
 * @param message like Toast.makeText(...)
 * @param length  like Toast.makeText(...)
 */// www  . java  2 s.co m
public static void showBackgroundToast(final Context context, final String message, final int length) {
    Handler handler = new Handler(context.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(context, message, length).show();
        }
    });
}

From source file:org.opensilk.music.playback.control.PlaybackController.java

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

From source file:com.musenkishi.wally.util.PaletteLoader.java

private static void setupHandlers(Context context) {
    HandlerThread handlerThread = new HandlerThread("palette-loader-background");
    handlerThread.start();/*from   ww  w.j  av  a 2s.  c  o m*/
    backgroundHandler = new Handler(handlerThread.getLooper(), sCallback);
    uiHandler = new Handler(context.getMainLooper(), sCallback);
}

From source file:jahirfiquitiva.iconshowcase.activities.ViewerActivity.java

private static Handler handler(Context context) {
    return new Handler(context.getMainLooper());
}

From source file:com.musenkishi.atelier.Atelier.java

private static void setupHandlers(Context context) {
    HandlerThread handlerThread = new HandlerThread("palette-loader-background");
    handlerThread.start();// ww  w  .ja  v  a 2s .c o  m
    backgroundHandler = new Handler(handlerThread.getLooper(), callback);
    uiHandler = new Handler(context.getMainLooper(), callback);
}

From source file:org.deviceconnect.message.intent.util.IntentAuthProcessor.java

/**
 * ????. Device Connect Manager???????????
 * Device Connect Manager??Intent??<br/>
 * ?????Intent????????UI?????<strong>????</strong><br/>
 * ??? AndroidManifest.xml??//from  w  ww  .  j  a  va 2 s. c o  m
 * {@link org.deviceconnect.message.intent.impl.io.IntentResponseReceiver}
 * ?????
 * 
 * @param context 
 * @param componentName Device Connect Manager????
 * @param packageName Authorization Create Client API?package?
 * @param appName Authorization Create Access Token
 *            API?applicationName?
 * @param scopes Authorization Create Access Token API?scope?
 * @param callback ??????
 */
public static void authorize(final Context context, final ComponentName componentName, final String packageName,
        final String appName, final String[] scopes, final AuthorizationHandler callback) {

    if (callback == null) {
        throw new IllegalArgumentException("Callback is null.");
    } else if (scopes == null || scopes.length == 0) {
        throw new IllegalArgumentException("No scopes.");
    } else if (componentName == null) {
        throw new IllegalArgumentException("componentName is null.");
    } else if (packageName == null) {
        throw new IllegalArgumentException("Package name is null.");
    } else if (appName == null) {
        throw new IllegalArgumentException("App name is null.");
    }

    if (Thread.currentThread().equals(context.getMainLooper().getThread())) {
        throw new IllegalStateException("DON'T CALL this method in UI thread.");
    }

    URIBuilder builder = new URIBuilder();
    // ??ComponentName?????????????????????
    builder.setHost("localhost");
    builder.setPort(0);
    builder.setScheme("http");

    builder.setProfile(AuthorizationProfileConstants.PROFILE_NAME);
    builder.setAttribute(AuthorizationProfileConstants.ATTRIBUTE_CREATE_CLIENT);
    builder.addParameter(AuthorizationProfileConstants.PARAM_PACKAGE, packageName);

    HttpUriRequest request = null;
    try {
        request = new HttpGet(builder.build());
    } catch (URISyntaxException e1) {
        throw new IllegalArgumentException("Invalid Param. Check parameters.");
    }
    // DefaultIntentClient?????Host?????????????????
    request.setHeader(HttpHeaders.HOST, componentName.flattenToShortString());

    ErrorCode error = null;
    HttpParams params = new BasicHttpParams();
    IntentConnectionParams.setContext(params, context);
    IntentConnectionParams.setComponent(params, componentName);
    DefaultIntentClient client = new DefaultIntentClient(params);

    do {
        try {
            JSONObject json = execute(client, request);
            error = checkResponse(json);
            if (error != null) {
                break;
            }

            String clientId = json.getString(AuthorizationProfileConstants.PARAM_CLIENT_ID);
            String clientSecret = json.getString(AuthorizationProfileConstants.PARAM_CLIENT_SECRET);
            String signature = AuthSignature.generateSignature(clientId,
                    AuthorizationProfileConstants.GrantType.AUTHORIZATION_CODE.getValue(), null, scopes,
                    clientSecret);
            // ???
            builder.setAttribute(AuthorizationProfileConstants.ATTRIBUTE_REQUEST_ACCESS_TOKEN);
            builder.addParameter(AuthorizationProfileConstants.PARAM_CLIENT_ID, clientId);
            builder.addParameter(AuthorizationProfileConstants.PARAM_SCOPE, combineStr(scopes));
            builder.addParameter(AuthorizationProfileConstants.PARAM_GRANT_TYPE,
                    AuthorizationProfileConstants.GrantType.AUTHORIZATION_CODE.getValue());
            builder.addParameter(AuthorizationProfileConstants.PARAM_SIGNATURE, signature);
            builder.addParameter(AuthorizationProfileConstants.PARAM_APPLICATION_NAME, appName);
            try {
                request = new HttpGet(builder.build());
            } catch (URISyntaxException e1) {
                error = ErrorCode.UNKNOWN;
                break;
            }

            // client??????????
            client.getConnectionManager().shutdown();
            client = new DefaultIntentClient(params);
            json = execute(client, request);
            error = checkResponse(json);
            if (error != null) {
                break;
            }
            String accessToken = json.getString(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN);
            callback.onAuthorized(clientId, clientSecret, accessToken);
        } catch (Exception e) {
            error = ErrorCode.UNKNOWN;
            e.printStackTrace();
            break;
        }
    } while (false);

    client.getConnectionManager().shutdown();

    if (error != null) {
        callback.onAuthFailed(error);
    }

}

From source file:com.veniosg.dir.mvvm.model.storage.operation.DeleteOperation.java

public DeleteOperation(Context context) {
    this.mainThreadHandler = new Handler(context.getMainLooper());
    this.toastDisplayer = new ToastDisplayer(context);
    this.context = context.getApplicationContext();

    runOnUi(() -> dialog = new ProgressDialog(context));
}

From source file:ai.api.unityhelper.RecognitionHelper.java

public void initialize(Context context) {
    this.context = context;
    handler = new Handler(context.getMainLooper());
}