Example usage for android.os Looper prepare

List of usage examples for android.os Looper prepare

Introduction

In this page you can find the example usage for android.os Looper prepare.

Prototype

public static void prepare() 

Source Link

Document

Initialize the current thread as a looper.

Usage

From source file:Main.java

static public void showToast(Context context, String msg) {
    Looper.prepare();
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    Looper.loop();
}

From source file:Main.java

public static void showToastOnMainThread(Context context, String msg) {
    Looper.prepare();
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    Looper.loop();
}

From source file:Main.java

public static Handler getApplicationHandler(Runnable runnable) {
    if (mApplicationHandler == null) {
        Looper.prepare();
        mApplicationHandler = new Handler(Looper.getMainLooper());
        Looper.loop();//from  w  w w.  j  a v a  2 s. c  o  m
    }
    return mApplicationHandler;
}

From source file:Main.java

public static void showToast(final String toast, final Context context) {
    new Thread(new Runnable() {

        @Override//from   ww w  . ja v a  2  s  .c om
        public void run() {
            Looper.prepare();
            //                Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
            Looper.loop();
        }
    }).start();
}

From source file:Main.java

public static void showToast(final String toast, final Context context) {
    new Thread(new Runnable() {

        @Override/* w  w  w  . ja va  2 s .  c  om*/
        public void run() {
            Looper.prepare();
            Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
            Looper.loop();
        }
    }).start();
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void removeAllCookiesV21() {
    final CookieManager cookieManager = CookieManager.getInstance();

    Looper looper = Looper.myLooper();//  w  w w . j a  v a2  s . c o  m
    boolean prepared = false;
    if (looper == null) {
        Looper.prepare();
        prepared = true;
    }

    // requires a looper
    cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
        @Override
        public void onReceiveValue(Boolean value) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    // is synchronous, run in background
                    cookieManager.flush();
                }
            };
            thread.start();
        }
    });

    if (prepared) {
        looper = Looper.myLooper();
        if (looper != null) {
            looper.quit();
        }
    }
}

From source file:org.apache.cordova.engine.crosswalk.XWalkExposedJsApi.java

@JavascriptInterface
public String exec(int bridgeSecret, String service, String action, String callbackId, String arguments)
        throws JSONException, IllegalAccessException {
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }//from   w w  w.j a v  a2s  .  c  o  m
    return bridge.jsExec(bridgeSecret, service, action, callbackId, arguments);
}

From source file:com.waz.zclient.testutils.FragmentTest.java

public FragmentTest(Class<A> activityType) {
    this.activityType = activityType;
    activityTestRule = new ActivityTestRule<>(activityType, false, false);
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }/*from   w  ww. j a va 2  s. co m*/
}

From source file:org.mobiletrial.license.connect.RestClient.java

public void post(final String path, final JSONObject requestJson, final OnRequestFinishedListener listener) {
    Thread t = new Thread() {
        public void run() {
            Looper.prepare(); //For Preparing Message Pool for the child Thread

            // Register Schemes for http and https
            final SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schemeRegistry.register(new Scheme("https", createAdditionalCertsSSLSocketFactory(), 443));

            final HttpParams params = new BasicHttpParams();
            final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
            HttpClient client = new DefaultHttpClient(cm, params);

            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit

            HttpResponse response;//  w  ww .  jav  a  2 s  .c o  m
            try {
                String urlStr = mServiceUrl.toExternalForm();
                if (urlStr.charAt(urlStr.length() - 1) != '/')
                    urlStr += "/";
                urlStr += path;
                URI actionURI = new URI(urlStr);

                HttpPost post = new HttpPost(actionURI);
                StringEntity se = new StringEntity(requestJson.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                post.setEntity(se);
                response = client.execute(post);

                /* Checking response */
                if (response == null) {
                    listener.gotError(ERROR_CONTACTING_SERVER);
                    Log.w(TAG, "Error contacting licensing server.");
                    return;
                }
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    Log.w(TAG, "An error has occurred on the licensing server.");
                    listener.gotError(ERROR_SERVER_FAILURE);
                    return;
                }

                /* Convert response to JSON */
                InputStream in = response.getEntity().getContent(); //Get the data in the entity
                String responseStr = inputstreamToString(in);
                listener.gotResponse(responseStr);

            } catch (ClientProtocolException e) {
                Log.w(TAG, "ClientProtocolExeption:  " + e.getLocalizedMessage());
                e.printStackTrace();
            } catch (IOException e) {
                Log.w(TAG, "Error on contacting server: " + e.getLocalizedMessage());
                listener.gotError(ERROR_CONTACTING_SERVER);
            } catch (URISyntaxException e) {
                //This shouldn't happen   
                Log.w(TAG, "Could not build URI.. Your service Url is propably not a valid Url:  "
                        + e.getLocalizedMessage());
                e.printStackTrace();
            }
            Looper.loop(); //Loop in the message queue
        }
    };
    t.start();
}

From source file:com.cellbots.communication.AppEngineCommChannel.java

@Override
public void listenForMessages(long waitTimeBetweenPolling, boolean returnStream) {
    stopReading = false;//  www  .ja  va  2  s  .com
    new Thread(new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            while (!stopReading) {
                try {
                    resetConnection();
                    PostMethod post = new PostMethod(mHttpCmdUrl);
                    post.setParameter("msg", "{}");
                    int result = post.execute(mHttpState, mConnection);
                    String response = post.getResponseBodyAsString();
                    int cmdStart = response.indexOf(startCmdStr);
                    if (cmdStart != -1) {
                        String command = response.substring(cmdStart + startCmdStr.length());
                        command = command.substring(0, command.indexOf("\""));
                        Log.e("command", command);
                        mMessageListener.onMessage(new CommMessage(command, null, "text/text", null, null,
                                mChannelName, CommunicationManager.CHANNEL_GAE));
                    }
                    // Thread.sleep(200);
                } catch (MalformedURLException e) {
                    Log.e(TAG, "Error processing URL: " + e.getMessage());
                } catch (IOException e) {
                    Log.e(TAG, "Error reading command from URL: " + mHttpCmdUrl + " : " + e.getMessage());
                }
            }
        }
    }).start();
}