Example usage for android.os Message sendToTarget

List of usage examples for android.os Message sendToTarget

Introduction

In this page you can find the example usage for android.os Message sendToTarget.

Prototype

public void sendToTarget() 

Source Link

Document

Sends this Message to the Handler specified by #getTarget .

Usage

From source file:fr.pasteque.client.utils.URLTextGetter.java

public static void getBinary(final String url, final Map<String, String> getParams, final Handler h) {
    new Thread() {
        @Override/*from www.  java2s .  co m*/
        public void run() {
            try {
                String fullUrl = url;
                if (getParams != null && getParams.size() > 0) {
                    fullUrl += "?";
                    for (String param : getParams.keySet()) {
                        fullUrl += URLEncoder.encode(param, "utf-8") + "="
                                + URLEncoder.encode(getParams.get(param), "utf-8") + "&";
                    }
                }
                if (fullUrl.endsWith("&")) {
                    fullUrl = fullUrl.substring(0, fullUrl.length() - 1);
                }
                HttpClient client = new DefaultHttpClient();
                HttpResponse response = null;
                HttpGet req = new HttpGet(fullUrl);
                response = client.execute(req);
                int status = response.getStatusLine().getStatusCode();
                if (status == HttpStatus.SC_OK) {
                    // Get http response
                    byte[] content = null;
                    try {
                        final int size = 10240;
                        ByteArrayOutputStream bos = new ByteArrayOutputStream(size);
                        byte[] buffer = new byte[size];
                        BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent(),
                                size);
                        int read = bis.read(buffer, 0, size);
                        while (read != -1) {
                            bos.write(buffer, 0, read);
                            read = bis.read(buffer, 0, size);
                        }
                        content = bos.toByteArray();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    if (h != null) {
                        Message m = h.obtainMessage();
                        m.what = SUCCESS;
                        m.obj = content;
                        m.sendToTarget();
                    }
                } else {
                    if (h != null) {
                        Message m = h.obtainMessage();
                        m.what = STATUS_NOK;
                        m.obj = new Integer(status);
                        m.sendToTarget();
                    }
                }
            } catch (IOException e) {
                if (h != null) {
                    Message m = h.obtainMessage();
                    m.what = ERROR;
                    m.obj = e;
                    m.sendToTarget();
                }
            }
        }
    }.start();
}

From source file:info.unyttig.helladroid.newzbin.NewzBinController.java

/**
 * Send back a update status message to the message handler.
 * This is used for short messages to the user, like "Search had no result" etc.
 * /*from  w w  w . j a va2  s  .  com*/
 * @param messageHandler
 * @param txt
 */
private static void sendUserMsg(final Handler messageHandler, final String txt) {
    Message msg = new Message();
    msg.setTarget(messageHandler);
    msg.what = MSG_NOTIFY_USER_ERROR;
    msg.obj = txt;
    msg.sendToTarget();
}

From source file:fr.pasteque.client.utils.URLTextGetter.java

public static void getText(final String url, final Map<String, String> getParams,
        final Map<String, String> postParams, final Handler h) {
    new Thread() {
        @Override/*from ww w . j a  v a 2 s  .  com*/
        public void run() {
            try {
                String fullUrl = url;
                if (getParams != null && getParams.size() > 0) {
                    fullUrl += "?";
                    for (String param : getParams.keySet()) {
                        fullUrl += URLEncoder.encode(param, "utf-8") + "="
                                + URLEncoder.encode(getParams.get(param), "utf-8") + "&";
                    }
                }
                if (fullUrl.endsWith("&")) {
                    fullUrl = fullUrl.substring(0, fullUrl.length() - 1);
                }
                HttpClient client = new DefaultHttpClient();
                HttpResponse response = null;
                if (postParams == null) {
                    HttpGet req = new HttpGet(fullUrl);
                    response = client.execute(req);
                } else {
                    HttpPost req = new HttpPost(fullUrl);
                    List<NameValuePair> args = new ArrayList<NameValuePair>();
                    for (String key : postParams.keySet()) {
                        String value = postParams.get(key);
                        args.add(new BasicNameValuePair(key, value));
                    }
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(args, HTTP.UTF_8);
                    req.setEntity(entity);
                    response = client.execute(req);
                }
                int status = response.getStatusLine().getStatusCode();
                if (status == HttpStatus.SC_OK) {
                    // Get http response
                    String content = "";
                    try {
                        final int size = 10240;
                        ByteArrayOutputStream bos = new ByteArrayOutputStream(size);
                        byte[] buffer = new byte[size];
                        BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent(),
                                size);
                        int read = bis.read(buffer, 0, size);
                        while (read != -1) {
                            bos.write(buffer, 0, read);
                            read = bis.read(buffer, 0, size);
                        }
                        content = new String(bos.toByteArray());
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    if (h != null) {
                        Message m = h.obtainMessage();
                        m.what = SUCCESS;
                        m.obj = content;
                        m.sendToTarget();
                    }
                } else {
                    if (h != null) {
                        Message m = h.obtainMessage();
                        m.what = STATUS_NOK;
                        m.obj = new Integer(status);
                        m.sendToTarget();
                    }
                }
            } catch (IOException e) {
                if (h != null) {
                    Message m = h.obtainMessage();
                    m.what = ERROR;
                    m.obj = e;
                    m.sendToTarget();
                }
            }
        }
    }.start();
}

From source file:com.hiqes.andele.Andele.java

private static void notifyDenied(ProtectedAction action) {
    Message msg = sHandler.obtainMessage(MSG_DENIED);

    msg.obj = action;/*from   ww w.  jav a  2 s.co m*/
    msg.sendToTarget();
}

From source file:com.hiqes.andele.Andele.java

private static void startSettingsApp(Context uiContext) {
    Message msg = sHandler.obtainMessage(MSG_GO_TO_SETTINGS);
    msg.obj = uiContext;//ww w  .j  a va2  s .c om
    msg.sendToTarget();
}

From source file:com.hiqes.andele.Andele.java

private static void showEducateUi(int reqCode, int actionIndex) {
    Message msg = sHandler.obtainMessage(MSG_SHOW_EDUCATE);

    msg.arg1 = reqCode;/*from  w  w  w . j a  v a  2s.  co  m*/
    msg.arg2 = actionIndex;
    msg.sendToTarget();
}

From source file:com.hiqes.andele.Andele.java

private static void showDeniedCritical(Request req, int actionIndex) {
    Message msg = sHandler.obtainMessage(MSG_SHOW_DENIED_CRITICAL);

    msg.obj = req;/* w  w  w  . j a v a 2  s . co m*/
    msg.arg1 = actionIndex;
    msg.sendToTarget();
}

From source file:com.hiqes.andele.Andele.java

private static void showDeniedFeedback(Request req, int actionIndex) {
    Message msg = sHandler.obtainMessage(MSG_SHOW_DENIED_FEEDBACK);

    msg.obj = req;/*from w  w w  .  j a v a2s.  c  o m*/
    msg.arg1 = actionIndex;
    msg.sendToTarget();
}

From source file:com.hiqes.andele.Andele.java

private static void showDeniedReminder(Request req, int actionIndex) {
    Message msg = sHandler.obtainMessage(MSG_SHOW_EDUCATE_REMINDER);

    msg.obj = req;/*w w  w.  ja va 2s. c o m*/
    msg.arg1 = actionIndex;
    msg.sendToTarget();
}

From source file:com.sabdroidex.controllers.couchpotato.CouchPotatoController.java

/**
 * Sends a message to the calling {@link Activity} to update it's status bar
 * /*  w ww.  j  av a2 s .  co m*/
 * @param messageHandler The message handler to be notified
 * @param text The text to write in the message
 */
private static void sendUpdateMessageStatus(Handler messageHandler, String text) {

    Message message = new Message();
    message.setTarget(messageHandler);
    message.what = MESSAGE.UPDATE.hashCode();
    message.obj = text;
    message.sendToTarget();
}