Example usage for android.os Message obtain

List of usage examples for android.os Message obtain

Introduction

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

Prototype

public static Message obtain() 

Source Link

Document

Return a new Message instance from the global pool.

Usage

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

private void sendResult(Intent intent, ArrayList<Product> products) {
    Bundle extras = intent.getExtras();//from   w w  w  . ja v  a2 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.fihmi.tools.minet.MiHttpResponseHandler.java

protected Message obtainMessage(int responseMessage, Object response, final int request_tag) {
    Message msg = null;/*from   w ww.  j  av  a  2s .  c om*/
    if (handler != null) {
        msg = this.handler.obtainMessage(responseMessage, response);
        msg.arg1 = request_tag;
    } else {
        msg = Message.obtain();
        msg.what = responseMessage;
        msg.obj = response;
        msg.arg1 = request_tag;
    }
    return msg;
}

From source file:com.jwork.spycamera.CameraTaskService.java

private void startVideoRecording() {
    CameraTaskService.setState(WHAT_START_VIDEO_RECORDING);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, createStopNotification("Running"));
    Message backMsg = Message.obtain();
    backMsg.what = MainController.WHAT_START_VIDEO;
    try {/*from   w w w .  j  a  v  a 2 s  .  c o  m*/
        this.outMessenger.send(backMsg);
    } catch (android.os.RemoteException e1) {
        log.w(this, e1);
    }

}

From source file:de.qspool.clementineremote.backend.ClementineService.java

@Override
public void onDestroy() {
    stopForeground(true);//from  ww  w  . jav a  2 s.co m
    if (App.mClementineConnection != null && App.mClementineConnection.isConnected()) {
        // Create a new request

        // Move the request to the message
        Message msg = Message.obtain();
        msg.obj = ClementineMessage.getMessage(MsgType.DISCONNECT);

        // Send the request to the thread
        App.mClementineConnection.mHandler.sendMessage(msg);
    }
    intteruptThread();
    App.mClementineConnection = null;
}

From source file:com.jwork.dhammapada.SearchFragment.java

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    log.v(this, "onListItemClick(pos:" + position + "|id:" + id);
    Message message = Message.obtain();
    message.what = Constant.WHAT_SELECT_VERSE;
    message.obj = v.getTag();//from  w  w w .  jav  a 2 s . c  o  m
    controller.executeMessage(message);
}

From source file:org.ntpsync.service.NtpSyncService.java

@Override
protected void onHandleIntent(Intent intent) {
    // lock cpu/*from   w  w  w. jav  a  2s .co m*/
    getLocks(this);
    lock();

    Bundle extras = intent.getExtras();
    if (extras == null) {
        Log.e(Constants.TAG, "Extra bundle is null!");
        return;
    }

    // fail if required keys are not present
    if (!extras.containsKey(EXTRA_ACTION)) {
        Log.e(Constants.TAG, "Extra bundle must contain a action!");
        return;
    }

    int action = extras.getInt(EXTRA_ACTION);

    // for these actions we get a result back which is send via the messenger and we require
    // a data bundle
    if (!(extras.containsKey(EXTRA_DATA))) {
        Log.e(Constants.TAG, "Extra bundle must contain a data bundle!");
        return;
    } else {
        mData = extras.getBundle(EXTRA_DATA);
    }

    boolean noMessenger = false;
    if (!(extras.containsKey(EXTRA_MESSENGER))) {
        Log.e(Constants.TAG, "No messenger present, using default result handling!");
        noMessenger = true;
    } else {
        mMessenger = (Messenger) extras.get(EXTRA_MESSENGER);
    }

    // get NTP server from preferences
    String ntpHostname = PreferenceHelper.getNtpServer(this);

    // default values
    int returnMessage = RETURN_GENERIC_ERROR;
    long offset = 0;

    // execute action from extra bundle
    switch (action) {
    case ACTION_QUERY:

        // return time to ui
        Bundle messageData = new Bundle();
        try {
            offset = NtpSyncUtils.query(ntpHostname);
            returnMessage = RETURN_OKAY;

            // calculate new time
            Date newTime = new Date(System.currentTimeMillis() + offset);

            messageData.putSerializable(MESSAGE_DATA_TIME, newTime);

            if (mData.containsKey(DATA_APPLY_DIRECTLY)) {
                if (mData.getBoolean(DATA_APPLY_DIRECTLY)) {
                    returnMessage = Utils.setTime(offset);
                }
            }
        } catch (IOException e) {
            returnMessage = RETURN_SERVER_TIMEOUT;
            Log.d(Constants.TAG, "Timeout on server!");
        }

        if (noMessenger && PreferenceHelper.getShowSyncToast(this)) {
            Message msg = Message.obtain();
            msg.arg1 = returnMessage;
            msg.setData(messageData);
            handleResult(msg);
        } else {
            sendMessageToHandler(returnMessage, messageData);
        }

        break;

    case ACTION_QUERY_DETAILED:

        String output = null;
        Bundle messageDataDetailedQuery = null;
        try {
            TimeInfo info = NtpSyncUtils.detailedQuery(ntpHostname);

            output = NtpSyncUtils.processResponse(info, this);

            // return detailed output to ui
            messageDataDetailedQuery = new Bundle();
            messageDataDetailedQuery.putSerializable(MESSAGE_DATA_DETAILED_OUTPUT, output);

            returnMessage = RETURN_OKAY;
        } catch (IOException e) {
            returnMessage = RETURN_SERVER_TIMEOUT;
            Log.d(Constants.TAG, "Timeout on server!");
        }

        if (noMessenger && PreferenceHelper.getShowSyncToast(this)) {
            Message msg = Message.obtain();
            msg.arg1 = returnMessage;
            msg.setData(messageDataDetailedQuery);
            handleResult(msg);
        } else {
            sendMessageToHandler(returnMessage, messageDataDetailedQuery);
        }

        break;

    default:
        break;

    }

    // unlock cpu
    unlock();
}

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 {//  w  w w  .ja  v a 2 s.  c om
            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();
    }
}

From source file:de.qspool.clementineremote.ui.Player.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        // Control the volume of clementine if enabled in the options
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (mSharedPref.getBoolean(App.SP_KEY_USE_VOLUMEKEYS, true)) {
                Message msgDown = Message.obtain();
                msgDown.obj = new RequestVolume(App.mClementine.getVolume() - 10);
                App.mClementineConnection.mHandler.sendMessage(msgDown);
                return true;
            }// w  w w . ja  v  a 2s .  co m
            break;
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (mSharedPref.getBoolean(App.SP_KEY_USE_VOLUMEKEYS, true)) {
                Message msgUp = Message.obtain();
                msgUp.obj = new RequestVolume(App.mClementine.getVolume() + 10);
                App.mClementineConnection.mHandler.sendMessage(msgUp);
                return true;
            }
            break;
        default:
            break;
        }
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.wehome.ctb.paintpanel.MainActivity.java

/**
 * app ?/*from  w ww.j  a v  a  2 s . c o  m*/
 * 
 */
public void checkNewVersion() {
    // ?Messagewhat1
    Message msg = Message.obtain();
    msg.what = MsgWhat.USER_LOGIN_DIALOG_STAUTS;
    URL url;//version.xml
    // version.xml???
    // ?XML XML?DOM??
    VersionXmlService service = VersionXmlService.getInstance();
    try {
        url = new URL(versioninfo);//version.xml?
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream inStream = connection.getInputStream();//???         
        mHashMap = service.loadVersionInfo(inStream);
        msg.obj = EnumUtil.Success;
    } catch (Exception e) {
        msg.obj = EnumUtil.ServerError;
    }

    // ????
    handler.sendMessage(msg);

}

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();
        msg.what = MESSAGE_ORBOT_STARTED;
        try {/*from  ww  w.  j a va 2  s . co m*/
            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();
    }
}