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: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. co 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:com.ztspeech.simutalk2.weibo.PostRecordTask.java

protected Integer doInBackground(Object... params) {
    /*//from  w ww . ja va  2s  .  c om
     * if (params == null || params.length == 0 || params.length != 5) {
     * handler.sendEmptyMessage(Constant.RESULT_FAILED_ARG_ERR); return 0; }
     */

    Kaixin kaixin = (Kaixin) params[0];
    handler = (Handler) params[1];
    String content = (String) params[2];
    InputStream in = (InputStream) params[3];
    try {
        // 
        Bundle bundle = new Bundle();
        bundle.putString("content", content);

        Map<String, Object> photoes = new HashMap<String, Object>();
        photoes.put("filename", in);

        String jsonResult = kaixin.uploadContent(context, RESTAPI_INTERFACE_POSTRECORD, bundle, photoes);

        if (jsonResult == null) {
            handler.sendEmptyMessage(Constant.RESULT_FAILED_NETWORK_ERR);
        } else {
            KaixinError kaixinError = Util.parseRequestError(jsonResult);
            if (kaixinError != null) {
                Message msg = Message.obtain();
                msg.what = Constant.RESULT_FAILED_REQUEST_ERR;
                msg.obj = kaixinError;
                handler.sendMessage(msg);
            } else {
                long rid = getRecordID(jsonResult);
                if (!isCancel) {
                    if (rid > 0) {
                        handler.sendEmptyMessage(Constant.RESULT_POST_RECORD_OK);
                    } else {
                        handler.sendEmptyMessage(Constant.RESULT_POST_RECORD_FAILED);
                    }
                } else {
                    handler.sendEmptyMessage(Constant.RESULT_USER_CANCEL_PROCESS);
                }
            }
        }
    } catch (MalformedURLException e1) {
        Log.e(TAG, "", e1);
        handler.sendEmptyMessage(Constant.RESULT_FAILED_MALFORMEDURL_ERR);
    } catch (IOException e1) {
        Log.e(TAG, "", e1);
        handler.sendEmptyMessage(Constant.RESULT_FAILED_NETWORK_ERR);
    } catch (Exception e1) {
        Log.e(TAG, "", e1);
        handler.sendEmptyMessage(Constant.RESULT_FAILED);
    }
    return 1;
}

From source file:com.cttapp.bby.mytlc.layer8apps.CalendarHandler.java

/************
 *  PURPOSE: Used to send our updates back to the main thread
 *  ARGUMENTS: String status// w  w w.j a v  a 2s.  c  o m
 *  RETURNS: VOID
 *  AUTHOR: Devin Collins <agent14709@gmail.com>
 *************/
private void updateStatus(String status) {
    Message msg = Message.obtain();
    Bundle data = new Bundle();
    data.putString("status", status);
    msg.setData(data);
    try {
        messenger.send(msg);
    } catch (Exception e) {
        // TODO: Error reporting?
    }
}

From source file:de.qspool.clementineremote.backend.receivers.ClementinePhoneStateCheck.java

@Override
public void onReceive(Context context, Intent intent) {
    if (App.getApp() == null || App.ClementineConnection == null || App.Clementine == null
            || !App.ClementineConnection.isConnected()) {
        return;// w  w  w  . j av a2  s .co  m
    }

    if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) {
        return;
    }

    if (ContextCompat.checkSelfPermission(context,
            Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    // Check if we need to change the volume
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(App.getApp());
    String volumeString = prefs.getString(SharedPreferencesKeys.SP_CALL_VOLUME, Clementine.DefaultCallVolume);
    int volume = Integer.parseInt(volumeString);

    // Get the pebble settings
    if (prefs.getBoolean(SharedPreferencesKeys.SP_LOWER_VOLUME, true)) {
        // Get the current state of the telephone
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        // On Lollipop, the state is broadcasted twice. Only process new states.
        if (lastPhoneState.equals(state))
            return;

        Message msg = Message.obtain();

        LastClementineState lastClementineState = new LastClementineState();
        lastClementineState.load();

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)
                || state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            // Only lower the volume once. When receiving a call, the state is RINGING. On pickup
            // OFFHOOK is broadcasted. So we only need to take action when we previously had the
            // IDLE state.
            if (lastPhoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                lastClementineState.volume = App.Clementine.getVolume();
                lastClementineState.state = App.Clementine.getState();
                lastClementineState.save();

                if (volume >= 0) {
                    msg.obj = ClementineMessageFactory.buildVolumeMessage(Integer.parseInt(volumeString));
                } else {
                    msg.obj = ClementineMessage.getMessage(ClementineRemoteProtocolBuffer.MsgType.PAUSE);
                }
            }
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            if (volume >= 0) {
                msg.obj = ClementineMessageFactory.buildVolumeMessage(lastClementineState.volume);
            } else {
                if (lastClementineState.state.equals(Clementine.State.PLAY)) {
                    msg.obj = ClementineMessage.getMessage(ClementineRemoteProtocolBuffer.MsgType.PLAY);
                }
            }
        }

        // Now send the message
        if (msg != null && msg.obj != null && App.ClementineConnection != null) {
            App.ClementineConnection.mHandler.sendMessage(msg);
        }

        lastPhoneState = state;
    }
}

From source file:com.crust87.ffmpegexecutorsample.MainActivity.java

private void bindEvent() {
    mVideoCropView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        @Override//from w  w w  .  j a  v a2 s .c  o m
        public void onPrepared(MediaPlayer mp) {
            mVideoCropView.start();
        }
    });

    mExecutor.setFFmepgExecutorListener(new FFmpegExecutor.FFmepgExecutorListener() {

        @Override
        public void onReadProcessLine(String line) {
            Log.d("TEST", line);

            Message message = Message.obtain();

            message.obj = line;
            message.setTarget(mMessageHandler);
            message.sendToTarget();
        }

        @Override
        public void onFinishProcess() {
            Toast.makeText(getApplicationContext(), "Finish FFmpeg Process", Toast.LENGTH_LONG).show();

            if (mProgressDialog != null) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
        }

        @Override
        public void onError(Exception e) {

        }
    });
}

From source file:angel.zhuoxiu.library.pusher.PusherChannel.java

public void dispatchEvents(String eventName, String eventData) {
    Bundle data = new Bundle();
    data.putString("eventName", eventName);
    data.putString("eventData", eventData);
    data.putString("channelName", mName);
    Message msg = Message.obtain();
    msg.setData(data);/*from   w w w . j a va2s  .com*/

    for (PusherCallback callback : mGlobalCallbacks) {
        callback.sendMessage(msg);
    }

    /* do we have a callback bound to that event? */
    if (mLocalCallbacks.containsKey(eventName)) {
        /* execute each callback */
        for (PusherCallback callback : mLocalCallbacks.get(eventName)) {
            callback.sendMessage(msg);
        }
    }

}

From source file:com.example.cuisoap.agrimac.homePage.homeFragment.java

public void test() {
    Message m = Message.obtain();
    JSONObject o = new JSONObject();

    try {//w w  w .j ava 2 s.  c om
        o.put("status", 0);
        JSONArray oo = new JSONArray("[{\n" + "    \"driveType\": \"1\",\n" + "    \"driverName\": \"p\",\n"
                + "    \"driverAge\": \"p\",\n" + "    \"driverGender\": \"female\",\n"
                + "    \"licenseType\": \"R\",\n"
                + "    \"driverLicense\": \"/sdcard/agrimac/driver2016_05_31_09_38_15.png\",\n"
                + "    \"machineType\": \"\",\n" + "    \"machineName\": \"p\",\n"
                + "    \"machinePower\": \"1\",\n" + "    \"passengerNum\": \"1\",\n"
                + "    \"wheelDistance\": \"1\",\n" + "    \"checkTime\": \"1\",\n"
                + "    \"payType\": \"loan\",\n" + "    \"machinePowerType\": \"\",\n"
                + "    \"machineLicense1\": \"/sdcard/agrimac/machineinfo2016_05_31_09_38_34.png\",\n"
                + "    \"machineLicense2\": \"/sdcard/agrimac/machineinfo2016_05_31_09_38_37.png\",\n"
                + "    \"leaseMonth\": \"1\",\n" + "    \"leaseTime\": \"1\",\n"
                + "    \"workCondition\": \"\",\n" + "    \"needType\": \"all\",\n"
                + "    \"houseType\": \"normal_house\",\n" + "    \"id\": \"1\"\n" + "}]");
        o.put("data", oo);
        m.obj = o;
        m.setTarget(h);
        m.sendToTarget();
    } catch (JSONException 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   www  . j a v  a 2 s  . co 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:com.cttapp.bby.mytlc.layer8apps.CalendarHandler.java

/************
 *  PURPOSE: Used to send our errors back to the main thread
 *  ARGUMENTS: String error//from  ww  w . j  a  v a 2  s.c om
 *  RETURNS: VOID
 *  AUTHOR: Devin Collins <agent14709@gmail.com>
 *************/
private void showError(String error) {
    Message msg = Message.obtain();
    Bundle data = new Bundle();
    data.putString("status", "ERROR");
    data.putString("error", error);
    msg.setData(data);
    try {
        messenger.send(msg);
    } catch (Exception e) {
        // TODO: Error reporting?
    }
}

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

@NonNull
@Override//from   w  w w.ja va2 s. c o m
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Messenger messenger = getArguments().getParcelable(ARG_MESSENGER);
    int title = getArguments().getInt(ARG_TITLE);
    final int message = getArguments().getInt(ARG_MESSAGE);
    int middleButton = getArguments().getInt(ARG_MIDDLE_BUTTON);
    final Activity activity = getActivity();

    ContextThemeWrapper theme = ThemeChanger.getDialogThemeWrapper(activity);

    CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(theme);
    builder.setTitle(title).setMessage(message);

    builder.setNegativeButton(R.string.orbot_start_dialog_cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Message msg = Message.obtain();
            msg.what = MESSAGE_DIALOG_CANCELLED;
            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);
            }

        }
    });

    builder.setNeutralButton(middleButton, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Message msg = new Message();
            msg.what = MESSAGE_MIDDLE_BUTTON;
            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);
            }
        }
    });

    builder.setPositiveButton(R.string.orbot_start_dialog_start, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // actual onClick defined in onStart, this is just to make the button appear
        }
    });

    return builder.show();
}