Example usage for android.os Handler obtainMessage

List of usage examples for android.os Handler obtainMessage

Introduction

In this page you can find the example usage for android.os Handler obtainMessage.

Prototype

public final Message obtainMessage() 

Source Link

Document

Returns a new android.os.Message Message from the global message pool.

Usage

From source file:Main.java

public static ResponseHandler<String> GetResponseHandlerInstance(final Handler handler) {
    final ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
        @Override// ww  w.  ja v  a  2s . c  om
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            Message message = handler.obtainMessage();
            Bundle bundle = new Bundle();
            StatusLine status = response.getStatusLine();
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                try {
                    result = InputStreamToString(entity.getContent());
                    bundle.putString("RESPONSE", result);
                    message.setData(bundle);
                    handler.sendMessage(message);
                } catch (IOException e) {
                    bundle.putString("RESPONSE", "Error - " + e.getMessage());
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            } else {
                bundle.putString("RESPONSE", "Error - " + response.getStatusLine().getReasonPhrase());
                message.setData(bundle);
                handler.sendMessage(message);
            }
            return result;
        }
    };

    return responseHandler;
}

From source file:com.squareup.picasso3.Utils.java

/**
 * Prior to Android 5, HandlerThread always keeps a stack local reference to the last message
 * that was sent to it. This method makes sure that stack local reference never stays there
 * for too long by sending new messages to it every second.
 *///from  w  w  w.j a va 2 s. c o  m
static void flushStackLocalLeaks(Looper looper) {
    Handler handler = new Handler(looper) {
        @Override
        public void handleMessage(Message msg) {
            sendMessageDelayed(obtainMessage(), THREAD_LEAK_CLEANING_MS);
        }
    };
    handler.sendMessageDelayed(handler.obtainMessage(), THREAD_LEAK_CLEANING_MS);
}

From source file:com.max2idea.android.fwknop.Fwknop.java

public static void sendHandlerMessage(Handler handler, int message_type) {
    Message msg1 = handler.obtainMessage();
    Bundle b = new Bundle();
    b.putInt("message_type", message_type);
    msg1.setData(b);//  w w w  .j  a  v  a2 s.c  om
    handler.sendMessage(msg1);
}

From source file:com.max2idea.android.fwknop.Fwknop.java

public static void sendHandlerMessage(Handler handler, int message_type, String message_var,
        String message_value) {//from ww  w .ja  v a  2 s .co  m
    Message msg1 = handler.obtainMessage();
    Bundle b = new Bundle();
    b.putInt("message_type", message_type);
    b.putString(message_var, message_value);
    msg1.setData(b);
    handler.sendMessage(msg1);
}

From source file:org.planetmono.dcuploader.SignOnRealName.java

public Runnable getMethodSignOn(final Application app, final Bundle b, final Handler resultHandler) {
    return new Runnable() {
        public void run() {
            Message m = resultHandler.obtainMessage();
            Bundle bm = m.getData();//from   w  w w. ja  va2 s  .c  o  m

            Log.d("dcuploader", "authenticating...");

            String encdata = b.getString("enc_data");
            String name = b.getString("name");
            String code1 = b.getString("code1");
            String code2 = b.getString("code2");

            HttpPost post = new HttpPost(AUTH_URL);
            List<NameValuePair> vlist = new ArrayList<NameValuePair>();
            vlist.add(new BasicNameValuePair("enc_data", encdata));
            vlist.add(new BasicNameValuePair("result_code", "1"));
            vlist.add(new BasicNameValuePair("contract_type", "S"));
            vlist.add(new BasicNameValuePair("au_chk", "F"));
            vlist.add(new BasicNameValuePair("name", name));
            vlist.add(new BasicNameValuePair("juminid1", code1));
            vlist.add(new BasicNameValuePair("juminid2", code2));
            try {
                post.setEntity(new UrlEncodedFormEntity(vlist));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            HttpResponse response = null;
            try {
                response = app.sendPostRequest(post);
            } catch (Exception e) {
                e.printStackTrace();

                bm.putBoolean("result", false);
                bm.putString("resultString", " ");
                resultHandler.sendMessage(m);
                return;
            }

            HttpEntity entity = response.getEntity();
            BufferedReader r;
            try {
                r = new BufferedReader(new InputStreamReader(entity.getContent(), "EUC-KR"));

                while (true) {
                    String line = r.readLine();
                    if (line == null)
                        break;

                    if (line.contains(" ??  ")) {
                        bm.putBoolean("result", false);
                        bm.putString("resultString", "?? ");
                        resultHandler.sendMessage(m);
                        return;
                    }
                }
            } catch (Exception e) {
                bm.putBoolean("result", false);
                bm.putString("resultString", " ");
                resultHandler.sendMessage(m);

                return;
            }

            /* abnormal status. traffic limit exceeded? */
            bm.putBoolean("result", true);
            resultHandler.sendMessage(m);
        }
    };
}

From source file:com.anton.gavel.ComplaintSubmission.java

public void submit() {

    //Body of your click handler
    Thread trd = new Thread(new Runnable() {
        @Override/*from   w w w . j  a va 2s.c o  m*/
        public void run() {
            String formPath = "http://www.hamilton.ca/Hamilton.Portal/Templates/COHShell.aspx?NRMODE=Published&NRORIGINALURL=%2fCityDepartments%2fCorporateServices%2fITS%2fForms%2bin%2bDevelopment%2fMunicipal%2bLaw%2bEnforcement%2bOnline%2bComplaint%2bForm%2ehtm&NRNODEGUID=%7b4319AA7C-7E5E-4D65-9F46-CCBEC9AB86E0%7d&NRCACHEHINT=Guest";
            String hideString = "document.getElementById('mainform').style.display = 'none';";
            //this javascript will be added to the page if we are successful - we can look
            //for it to identify whether our form was successfully submitted

            // Create a new HttpClient and Post Header
            HttpClient httpClient = new DefaultHttpClient();

            ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();

            for (String key : post_params.keySet()) {
                parameters.add(new BasicNameValuePair(key, post_params.get(key)));
                String value = post_params.get(key);
                if (value != null && value.length() > 0) {
                    value = value.substring(0, Math.min(70, value.length()));
                }
                Log.d("Runs", "key: '" + key + "', value: '" + value);
            }
            //convert to a list of name-value pairs (dum, dee dum  -  duuuuumm...)

            HttpPost request = new HttpPost(formPath);

            //set up handler/bundle to give output to main thread
            Handler handler = ((GavelMain) mContext).submissionHandler;
            Message msg = handler.obtainMessage();
            Bundle bundle = new Bundle();

            try {
                request.setEntity(new UrlEncodedFormEntity(parameters));

                HttpResponse httpResponse = httpClient.execute(request);
                HttpEntity responseEntity = httpResponse.getEntity();

                String response = responseEntity == null ? ""
                        : EntityUtils.toString(responseEntity, EntityUtils.getContentCharSet(responseEntity));
                //read the html page posted in response to our request

                appendLog(response); // logs html page response to a text file on sd card

                bundle.putBoolean("succeeded",
                        httpResponse.getStatusLine().getStatusCode() == 200 && response.contains(hideString));

                msg.setData(bundle);
                handler.sendMessage(msg);
            } catch (ClientProtocolException e) {
                bundle.putBoolean("succeeded", false);
                msg.setData(bundle);
                handler.sendMessage(msg);
            } catch (IOException e) {
                bundle.putBoolean("succeeded", false);
                msg.setData(bundle);
                handler.sendMessage(msg);
            } //code to do the HTTP request            
        }
    });
    trd.start();

    //

    return;

}

From source file:org.planetmono.dcuploader.SignOnRealName.java

public Runnable getMethodSignOff(final Application app, final Handler resultHandler) {
    return new Runnable() {
        public void run() {
            // AFAIK there is no way to sign off. Let the session die alone...
            Message m = resultHandler.obtainMessage();
            m.getData().putBoolean("result", true);
            resultHandler.handleMessage(m);
        }//from   ww  w  . j av a2 s  . c  om
    };
}

From source file:com.hoccer.api.android.AsyncLinccer.java

public void asyncShare(final String mode, final JSONObject payload, final Handler handler) {
    new Thread(new Runnable() {
        public void run() {

            Message msg = handler.obtainMessage();
            try {
                handler.handleMessage(handler.obtainMessage(MessageType.SEARCHING));
                msg.obj = share(mode, payload);

                if (msg.obj != null) {
                    msg.what = MessageType.SHARED;
                } else {
                    msg.what = MessageType.NOTHING_SHARED;
                }//from ww w.ja v  a  2  s .  co  m
            } catch (BadModeException e) {
                msg.what = MessageType.BAD_MODE;
                msg.obj = e;
            } catch (ClientActionException e) {
                msg.what = MessageType.BAD_CLIENT_ACTION;
                msg.obj = e;
            } catch (CollidingActionsException e) {
                msg.what = MessageType.COLLISION;
                msg.obj = e;
            } catch (Exception e) {
                msg.what = MessageType.UNKNOWN_EXCEPTION;
                msg.obj = e;
            }

            Log.v("Linccer", msg.what + " " + msg.obj);

            handler.handleMessage(msg);
        }
    }).start();
}

From source file:com.hoccer.api.android.AsyncLinccer.java

public void asyncReceive(final String mode, final Handler handler) {
    new Thread(new Runnable() {
        public void run() {

            Message msg = handler.obtainMessage();
            try {
                handler.handleMessage(handler.obtainMessage(MessageType.SEARCHING));
                msg.obj = receive(mode);

                if (msg.obj != null) {
                    msg.what = MessageType.RECEIVED;
                } else {
                    msg.what = MessageType.NOTHING_RECEIVED;
                }/*from w ww . ja va  2 s.  c  om*/
            } catch (BadModeException e) {
                msg.what = MessageType.BAD_MODE;
                msg.obj = e;
            } catch (ClientActionException e) {
                msg.what = MessageType.BAD_CLIENT_ACTION;
                msg.obj = e;
            } catch (CollidingActionsException e) {
                msg.what = MessageType.COLLISION;
                msg.obj = e;
            } catch (Exception e) {
                msg.what = MessageType.UNKNOWN_EXCEPTION;
                msg.obj = e;
            }

            Log.v("Linccer", msg.what + " " + msg.obj);

            handler.handleMessage(msg);
        }
    }).start();

}

From source file:ch.ethz.inf.vs.android.g54.a4.types.Building.java

/**
 * Asynchronously get free rooms based on floor and time constraints
 * A message will be dispatched to the handler informing of the status
 * In case of failure, the exception string is passed through the message key of the bundle
 * @param f Floor constraint/*from  ww  w  . ja  va 2  s  .  co m*/
 * @param start time constraint in quarter hours
 * @param end time constraint in quarter hours
 * @param handler Handler that will get the success/failure message with this object
 */
public void getFreeRoomsAsync(final Floor f, final Float start, final Float end, final Handler handler) {
    new Thread() {
        public void run() {
            Message m = handler.obtainMessage();
            try {
                m.obj = getFreeRoom(f, start, end);
                m.what = MessageStatus.SUCCESS.ordinal();
            } catch (Exception e) {
                m.what = MessageStatus.FAILURE.ordinal();
                Bundle b = new Bundle();
                b.putString("message", e.getMessage());
                m.setData(b);
            } finally {
                handler.sendMessage(m);
            }
        }
    }.start();
}