Example usage for android.os Messenger send

List of usage examples for android.os Messenger send

Introduction

In this page you can find the example usage for android.os Messenger send.

Prototype

public void send(Message message) throws RemoteException 

Source Link

Document

Send a Message to this Messenger's Handler.

Usage

From source file:Main.java

public static void sendMsg2Client(Messenger messenger, String sKey, String sObjParam, int iMsg) {
    if (messenger == null) {
        return;//www .  j  a  va 2  s .c  om
    }

    try {
        Bundle b = new Bundle();

        if (null != sKey) {
            b.putString(sKey, sObjParam);
        }

        Message msgBack = Message.obtain(null, iMsg);

        if (msgBack != null) {
            msgBack.setData(b);
            messenger.send(msgBack);
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

From source file:com.darshancomputing.alockblock.ALockBlockService.java

private static void sendClientMessage(Messenger clientMessenger, int what, Bundle data) {
    Message outgoing = Message.obtain();
    outgoing.what = what;//  ww  w .  j  av a  2s  . com
    outgoing.replyTo = messenger;
    outgoing.setData(data);
    try {
        clientMessenger.send(outgoing);
    } catch (android.os.RemoteException e) {
    }
}

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

static void sendReply(Messenger messenger, int what, int requestId, int arg, Object obj, Bundle data) {
    Message msg = Message.obtain();/* w  w w  .  j a va 2 s  . c o  m*/
    msg.what = what;
    msg.arg1 = requestId;
    msg.arg2 = arg;
    msg.obj = obj;
    msg.setData(data);
    try {
        messenger.send(msg);
    } catch (DeadObjectException ex) {
        // The client died.
    } catch (RemoteException ex) {
        Log.e(TAG, "Could not send message to " + getClientId(messenger), ex);
    }
}

From source file:by.zatta.pilight.connection.ConnectionService.java

/**
 * Send the data to all clients./*from w w  w. jav a 2  s .c  om*/
 * 
 * @param message
 *            The message to send.
 */
private static void sendMessageToUI(int what, String message) {
    // Log.v(TAG, "sent message called, clients attached: " + Integer.toString(mClients.size()));
    Iterator<Messenger> messengerIterator = mClients.iterator();
    while (messengerIterator.hasNext()) {
        Messenger messenger = messengerIterator.next();
        try {
            Bundle bundle = new Bundle();
            bundle.setClassLoader(aCtx.getClassLoader());
            switch (what) {
            case MSG_SET_STATUS:
                // Log.v(TAG, "setting status: " + message);
                bundle.putString("status", message);
                Message msg_string = Message.obtain(null, MSG_SET_STATUS);
                msg_string.setData(bundle);
                messenger.send(msg_string);
                break;
            case MSG_SET_BUNDLE:
                if (!mDevices.isEmpty()) {
                    // Log.v(TAG, "putting mDevices");
                    bundle.putParcelableArrayList("config", (ArrayList<? extends Parcelable>) mDevices);
                    Message msg = Message.obtain(null, MSG_SET_BUNDLE);
                    msg.setData(bundle);
                    messenger.send(msg);
                }
                break;
            }
        } catch (RemoteException e) {
            Log.w(TAG, "mClients.remove called");
            mClients.remove(messenger);
        }
    }
}

From source file:com.commonsware.android.tuning.downloader.Downloader.java

@Override
public void onHandleIntent(Intent i) {
    HttpGet getMethod = new HttpGet(i.getData().toString());
    int result = Activity.RESULT_CANCELED;

    try {//  www .  j  a  v a 2 s . c om
        ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
        byte[] responseBody = client.execute(getMethod, responseHandler);
        File output = new File(Environment.getExternalStorageDirectory(), i.getData().getLastPathSegment());

        if (output.exists()) {
            output.delete();
        }

        FileOutputStream fos = new FileOutputStream(output.getPath());

        fos.write(responseBody);
        fos.close();
        result = Activity.RESULT_OK;
    } catch (IOException e2) {
        Log.e(getClass().getName(), "Exception in download", e2);
    }

    Bundle extras = i.getExtras();

    if (extras != null) {
        Messenger messenger = (Messenger) extras.get(EXTRA_MESSENGER);
        Message msg = Message.obtain();

        msg.arg1 = result;

        try {
            messenger.send(msg);
        } catch (android.os.RemoteException e1) {
            Log.w(getClass().getName(), "Exception sending message", e1);
        }
    }
}

From source file:org.sufficientlysecure.keychain.ui.dialog.OrbotStartDialogFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == ORBOT_REQUEST_CODE) {
        // assume Orbot was started
        final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER);

        Message msg = Message.obtain();/*from   w  w  w .  j  a  v a2 s . co m*/
        msg.what = MESSAGE_ORBOT_STARTED;
        try {
            messenger.send(msg);
        } catch (RemoteException e) {
            Log.w(Constants.TAG, "Exception sending message, Is handler present?", e);
        } catch (NullPointerException e) {
            Log.w(Constants.TAG, "Messenger is null!", e);
        }
        dismiss();
    }
}

From source file:com.gmail.at.faint545.services.DataQueueService.java

@Override
protected void onHandleIntent(Intent intent) {
    String url = intent.getStringExtra("url");
    String api = intent.getStringExtra("api");
    StringBuilder results = new StringBuilder();

    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);

    ArrayList<NameValuePair> arguments = new ArrayList<NameValuePair>();
    arguments.add(new BasicNameValuePair(SabnzbdConstants.APIKEY, api));
    arguments.add(new BasicNameValuePair(SabnzbdConstants.OUTPUT, SabnzbdConstants.OUTPUT_JSON));
    arguments.add(new BasicNameValuePair(SabnzbdConstants.MODE, SabnzbdConstants.MODE_QUEUE));

    try {//  ww w .j a v  a2s. c  o m
        request.setEntity(new UrlEncodedFormEntity(arguments));
        HttpResponse result = client.execute(request);
        InputStream inStream = result.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(inStream), 8);
        String line;
        while ((line = br.readLine()) != null) {
            if (line.length() > 0) {
                results.append(line);
            }
        }
        br.close();
        inStream.close();

        Bundle extras = intent.getExtras();
        Messenger messenger = (Messenger) extras.get("messenger");
        Message message = Message.obtain();

        Bundle resultsBundle = new Bundle();
        resultsBundle.putString("results", results.toString());
        message.setData(resultsBundle);

        messenger.send(message);
        stopSelf();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

From source file:fr.julienvermet.bugdroid.service.CommentIntentService.java

private void sendResult(Intent intent, int statusCode, String result) {
    Bundle extras = intent.getExtras();/*from ww w .ja va 2 s  . c o m*/
    Messenger messenger = (Messenger) extras.get(MESSENGER);
    if (messenger != null) {
        Message msg = Message.obtain();
        Bundle data = new Bundle();
        data.putInt(STATUS_CODE, statusCode);
        data.putString(RESULT, result);
        msg.setData(data);
        try {
            messenger.send(msg);
        } catch (android.os.RemoteException e1) {
            Log.w(getClass().getName(), "Exception sending message", e1);
        }
    }
}

From source file:fr.julienvermet.bugdroid.service.ProductsIntentService.java

private void sendResult(Intent intent, ArrayList<Product> products) {
    Bundle extras = intent.getExtras();/*from  ww w .  j av  a  2  s .  c  om*/
    Messenger messenger = (Messenger) extras.get(MESSENGER);
    if (messenger != null) {
        Message msg = Message.obtain();
        Bundle data = new Bundle();
        data.putSerializable(PRODUCTS, products);
        msg.setData(data);
        try {
            messenger.send(msg);
        } catch (android.os.RemoteException e1) {
            Log.w(getClass().getName(), "Exception sending message", e1);
        }
    }
}

From source file:com.owncloud.activity.Downloader.java

public void downloadFile(Intent i) {
    if (isOnline()) {
        String url = i.getData().toString();
        GetMethod gm = new GetMethod(url);

        try {//ww  w .  java  2 s. c o  m
            int status = BaseActivity.httpClient.executeMethod(gm);
            //            Toast.makeText(getApplicationContext(), String.valueOf(status),2).show();
            Log.e("HttpStatus", "==============>" + String.valueOf(status));
            if (status == HttpStatus.SC_OK) {
                InputStream input = gm.getResponseBodyAsStream();

                String fileName = new StringBuffer(url).reverse().toString();
                String[] fileNameArray = fileName.split("/");
                fileName = new StringBuffer(fileNameArray[0]).reverse().toString();

                fileName = URLDecoder.decode(fileName);
                File folder = new File(BaseActivity.mDownloadDest);
                boolean success = false;
                if (!folder.exists()) {
                    success = folder.mkdir();
                }
                if (!success) {
                    // Do something on success
                    File file = new File(BaseActivity.mDownloadDest, fileName);
                    OutputStream fOut = new FileOutputStream(file);

                    int byteCount = 0;
                    byte[] buffer = new byte[4096];
                    int bytesRead = -1;

                    while ((bytesRead = input.read(buffer)) != -1) {
                        fOut.write(buffer, 0, bytesRead);
                        byteCount += bytesRead;
                        //                     DashBoardActivity.updateNotation();
                    }

                    fOut.flush();
                    fOut.close();
                } else {
                    // Do something else on failure
                    Log.d("Download Prob..", String.valueOf(success) + ", Some problem in folder creating");
                }

            }
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        gm.releaseConnection();

        Bundle extras = i.getExtras();

        if (extras != null) {
            Messenger messenger = (Messenger) extras.get(EXTRA_MESSENGER);
            Message msg = Message.obtain();

            msg.arg1 = Activity.RESULT_OK;

            try {
                messenger.send(msg);
            } catch (android.os.RemoteException e1) {
                Log.w(getClass().getName(), "Exception sending message", e1);
            }
        }
    } else {
        WebNetworkAlert();
    }
}