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(Handler h, int what) 

Source Link

Document

Same as #obtain() , but sets the values for both target and what members on the Message.

Usage

From source file:app.axe.imooc.zxing.app.CaptureActivity.java

private void handleDecodeExternally(Result rawResult, Bitmap barcode) {
    viewfinderView.drawResultBitmap(barcode);

    // Since this message will only be shown for a second, just tell the
    // user what kind of
    // barcode was found (e.g. contact info) rather than the full contents,
    // which they won't
    // have time to read.

    if (source == Source.NATIVE_APP_INTENT) {
        // Hand back whatever action they requested - this can be changed to
        // Intents.Scan.ACTION when
        // the deprecated intent is retired.
        Intent intent = new Intent(getIntent().getAction());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
        intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());
        Message message = Message.obtain(handler, R.id.return_scan_result);
        message.obj = intent;/*from  w  w  w .j a  v  a 2  s  . c  o m*/
        handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    } else if (source == Source.PRODUCT_SEARCH_LINK) {
        // Reformulate the URL which triggered us into a query, so that the
        // request goes to the same
        // TLD as the scan URL.
        Message message = Message.obtain(handler, R.id.launch_product_query);
        // message.obj = sourceUrl.substring(0, end) + "?q=" +
        // resultHandler.getDisplayContents().toString()
        // + "&source=zxing";
        handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    } else if (source == Source.ZXING_LINK) {
        // Replace each occurrence of RETURN_CODE_PLACEHOLDER in the
        // returnUrlTemplate
        // with the scanned code. This allows both queries and REST-style
        // URLs to work.
        Message message = Message.obtain(handler, R.id.launch_product_query);
        // message.obj = returnUrlTemplate.replace(RETURN_CODE_PLACEHOLDER,
        // resultHandler.getDisplayContents()
        // .toString());
        handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    }
}

From source file:com.smithdtyler.prettygoodmusicplayer.NowPlaying.java

void doUnbindService() {
    Log.i(TAG, "Unbinding the service!");
    if (mIsBound) {
        // If we have received the service, and hence registered with it,
        // then now is the time to unregister.
        if (mService != null) {
            try {
                Message msg = Message.obtain(null, MusicPlaybackService.MSG_UNREGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);/*from w w w .  ja  va2  s  . c  o m*/
            } catch (RemoteException e) {
                // There is nothing special we need to do if the service has
                // crashed.
            }
        }

        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}

From source file:com.tenforwardconsulting.cordova.BackgroundGeolocationPlugin.java

void doUnbindService() {
    if (isBound) {
        log.info("Unbinding from bg service");
        // If we have received the service, and hence registered with
        // it, then now is the time to unregister.
        if (mService != null) {
            try {
                Message msg = Message.obtain(null, LocationService.MSG_UNREGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);/*ww  w.  j a  v a 2 s  .c o  m*/
            } catch (RemoteException e) {
                // There is nothing special we need to do if the service
                // has crashed.
            }

            // Detach our existing connection.
            Activity activity = getActivity();
            activity.unbindService(mConnection);
            isBound = false;
        }
    }
}

From source file:org.openbmap.activities.HostActivity.java

/**
 * Stops GPS und wireless logger services.
 *//*from w w  w.  j  a  v a 2 s.c o m*/
private void stopServices() {
    Log.d(TAG, "Stopping Services");
    // Brute force: specific ServiceManager can be null, if service hasn't been started
    // Service status is ignored, stop message is send regardless of whether started or not
    try {
        mPositionServiceManager.sendAsync(Message.obtain(null, RadioBeacon.MSG_STOP_TRACKING));
        // deactivated: let's call this from the service itself
        // positionServiceManager.unbindAndStop();
    } catch (final Exception e) {
        Log.w(TAG, "Failed to stop gpsPositionServiceManager. Is service runnign?" /*+ e.getMessage()*/);
        //e.printStackTrace();
    }

    try {
        mWirelessServiceManager.sendAsync(Message.obtain(null, RadioBeacon.MSG_STOP_TRACKING));
        // deactivated: let's call this from the service itself
        // wirelessServiceManager.unbindAndStop();
    } catch (final Exception e) {
        Log.w(TAG, "Failed to stop wirelessServiceManager. Is service running?" /*+ e.getMessage()*/);
        //e.printStackTrace();
    }

    try {
        mGpxLoggerServiceManager.sendAsync(Message.obtain(null, RadioBeacon.MSG_STOP_TRACKING));
        // deactivated: let's call this from the service itself
        // gpxLoggerServiceManager.unbindAndStop();
    } catch (final Exception e) {
        Log.w(TAG, "Failed to stop gpxLoggerServiceManager. Is service running?" /*+ e.getMessage()*/);
        //e.printStackTrace();
    }
}

From source file:org.navitproject.navit.Navit.java

private void parseNavigationURI(String schemeSpecificPart) {
    String naviData[] = schemeSpecificPart.split("&");
    Pattern p = Pattern.compile("(.*)=(.*)");
    Map<String, String> params = new HashMap<String, String>();
    for (int count = 0; count < naviData.length; count++) {
        Matcher m = p.matcher(naviData[count]);

        if (m.matches()) {
            params.put(m.group(1), m.group(2));
        }//from www  . j a v a2  s  .c  o  m
    }

    // d: google.navigation:q=blabla-strasse # (this happens when you are offline, or from contacts)
    // a: google.navigation:ll=48.25676,16.643&q=blabla-strasse
    // c: google.navigation:ll=48.25676,16.643
    // b: google.navigation:q=48.25676,16.643

    Float lat;
    Float lon;
    Bundle b = new Bundle();

    String geoString = params.get("ll");
    if (geoString != null) {
        String address = params.get("q");
        if (address != null)
            b.putString("q", address);
    } else {
        geoString = params.get("q");
    }

    if (geoString != null) {
        if (geoString.matches("^[+-]{0,1}\\d+(|\\.\\d*),[+-]{0,1}\\d+(|\\.\\d*)$")) {
            String geo[] = geoString.split(",");
            if (geo.length == 2) {
                try {
                    lat = Float.valueOf(geo[0]);
                    lon = Float.valueOf(geo[1]);
                    b.putFloat("lat", lat);
                    b.putFloat("lon", lon);
                    Message msg = Message.obtain(N_NavitGraphics.callback_handler,
                            NavitGraphics.msg_type.CLB_SET_DESTINATION.ordinal());

                    msg.setData(b);
                    msg.sendToTarget();
                    Log.e("Navit", "target found (b): " + geoString);
                } catch (NumberFormatException e) {
                } // nothing to do here
            }
        } else {
            start_targetsearch_from_intent(geoString);
        }
    }
}

From source file:com.vkassin.mtrade.Common.java

public static void login(final Context ctx) {

    // ctx = Common.app_ctx;
    Common.connected = false;//  w w  w. j av  a 2  s.c o  m

    if (inLogin)
        return;

    inLogin = true;

    if (Common.mainActivity != null)
        Common.mainActivity.handler.sendMessage(
                Message.obtain(Common.mainActivity.handler, Common.mainActivity.DISMISS_PROGRESS_DIALOG));

    // while(true) {

    final Dialog dialog = new Dialog(ctx);
    dialog.setContentView(R.layout.login_dialog);
    dialog.setTitle(R.string.LoginDialogTitle);
    dialog.setCancelable(false);

    final EditText nametxt = (EditText) dialog.findViewById(R.id.loginnameedit);
    final EditText passtxt = (EditText) dialog.findViewById(R.id.passwordedit);
    final EditText passtxt1 = (EditText) dialog.findViewById(R.id.passwordedit1);
    final EditText passtxt2 = (EditText) dialog.findViewById(R.id.passwordedit2);
    final EditText mailtxt = (EditText) dialog.findViewById(R.id.emailedit2);

    String nam = myaccount.get("name");
    Common.oldName = nam;

    if (nam != null) {

        nametxt.setText(nam);
        passtxt.requestFocus();
    }

    Button customDialog_Register = (Button) dialog.findViewById(R.id.goregister);
    customDialog_Register.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View arg0) {

            dialog.setTitle(R.string.LoginDialogTitle1);
            final LinearLayout layreg = (LinearLayout) dialog.findViewById(R.id.reglayout354);
            layreg.setVisibility(View.VISIBLE);
            final LinearLayout laylog = (LinearLayout) dialog.findViewById(R.id.loginlayout543);
            laylog.setVisibility(View.GONE);
        }

    });

    Button customDialog_Register1 = (Button) dialog.findViewById(R.id.goregister1);
    customDialog_Register1.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View arg0) {

            if (mailtxt.getText().length() < 1) {

                Toast toast = Toast.makeText(mainActivity, R.string.CorrectEmail, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();

                return;
            }

            if (passtxt1.getText().length() < 1) {

                Toast toast = Toast.makeText(mainActivity, R.string.CorrectPassword, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
                return;
            }

            if (!passtxt2.getText().toString().equals(passtxt1.getText().toString())) {

                Toast toast = Toast.makeText(mainActivity, R.string.CorrectPassword1, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
                return;
            }

            try {

                Socket sock = new Socket(ip_addr, port_register);

                JSONObject msg = new JSONObject();
                msg.put("objType", Common.MSG_REGISTER);
                msg.put("time", Calendar.getInstance().getTimeInMillis());
                msg.put("user", mailtxt.getText().toString());
                msg.put("passwd", passtxt1.getText().toString());
                msg.put("version", Common.PROTOCOL_VERSION);
                msg.put("sign", sign(mailtxt.getText().toString(), passtxt1.getText().toString()));

                byte[] array = msg.toString().getBytes();
                ByteBuffer buff = ByteBuffer.allocate(array.length + 4);
                buff.putInt(array.length);
                buff.put(array);
                sock.getOutputStream().write(buff.array());

                ByteBuffer buff1 = ByteBuffer.allocate(4);
                buff1.put(readMsg(sock.getInputStream(), 4));
                buff1.position(0);
                int pkgSize = buff1.getInt();
                // Log.i(TAG, "size = "+pkgSize);
                String s = new String(readMsg(sock.getInputStream(), pkgSize));

                sock.close();

                JSONObject jo = new JSONObject(s);

                Log.i(TAG, "register answer = " + jo);

                int t = jo.getInt("status");
                switch (t) {

                case 1:
                    Toast toast = Toast.makeText(mainActivity, R.string.RegisterStatus1, Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();

                    dialog.setTitle(R.string.LoginDialogTitle);
                    final LinearLayout layreg = (LinearLayout) dialog.findViewById(R.id.reglayout354);
                    layreg.setVisibility(View.GONE);
                    final LinearLayout laylog = (LinearLayout) dialog.findViewById(R.id.loginlayout543);
                    laylog.setVisibility(View.VISIBLE);

                    nametxt.setText(mailtxt.getText());
                    break;

                case -2:
                    Toast toast1 = Toast.makeText(mainActivity, R.string.RegisterStatus3, Toast.LENGTH_LONG);
                    toast1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast1.show();
                    break;

                default:
                    Toast toast2 = Toast.makeText(mainActivity, R.string.RegisterStatus2, Toast.LENGTH_LONG);
                    toast2.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast2.show();
                    break;

                }

            } catch (Exception e) {

                e.printStackTrace();
                Log.e(TAG, "Error in registration process!!", e);

                Toast toast = Toast.makeText(mainActivity, R.string.ConnectError, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();

            }

        }

    });

    Button customDialog_CancelReg = (Button) dialog.findViewById(R.id.cancelreg);
    customDialog_CancelReg.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View arg0) {

            dialog.setTitle(R.string.LoginDialogTitle);
            final LinearLayout layreg = (LinearLayout) dialog.findViewById(R.id.reglayout354);
            layreg.setVisibility(View.GONE);
            final LinearLayout laylog = (LinearLayout) dialog.findViewById(R.id.loginlayout543);
            laylog.setVisibility(View.VISIBLE);

        }

    });

    Button customDialog_Dismiss = (Button) dialog.findViewById(R.id.gologin);
    customDialog_Dismiss.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View arg0) {

            final RadioButton bu0 = (RadioButton) dialog.findViewById(R.id.lradio0);
            Common.isSSL = bu0.isChecked();

            inLogin = false;

            JSONObject msg = new JSONObject();
            try {

                msg.put("objType", Common.LOGOUT);
                msg.put("time", Calendar.getInstance().getTimeInMillis());
                msg.put("version", Common.PROTOCOL_VERSION);
                msg.put("status", 1);
                mainActivity.writeJSONMsg(msg);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, "Error! Cannot create JSON logout object", e);
            }

            myaccount.put("name", nametxt.getText().toString());
            myaccount.put("password", passtxt.getText().toString());

            Log.i(TAG,
                    "myaccount username: " + myaccount.get("name") + " password: " + myaccount.get("password"));

            dialog.dismiss();
            mainActivity.stop();
            // saveAccountDetails();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            loginFromDialog = true;
            //            mainActivity.refresh();

            Common.keypassword(ctx);
        }

    });

    dialog.show();
    // Common.confChanged = false;
    // }//while(true);

}

From source file:com.googlecode.networklog.NetworkLog.java

public static void toggleServiceForeground(Boolean value) {
    MyLog.d("toggleServiceForeground " + value);

    if (isBound && service != null) {
        try {//ww  w  . jav  a 2  s  . co  m
            Message msg = Message.obtain(null, NetworkLogService.MSG_TOGGLE_FOREGROUND);
            msg.obj = value;
            service.send(msg);
        } catch (RemoteException e) {
            /* do nothing */
            Log.d("NetworkLog", "RemoteException toggling foreground", e);
        }
    }
}

From source file:com.roymam.android.nilsplus.ui.NiLSActivity.java

private void checkNiLSPlusLicense() {
    // if so - request a license
    Intent licenseService = new Intent();
    licenseService.setComponent(//w  w  w .j ava 2 s . co  m
            new ComponentName("com.roymam.android.nilsplus", "com.roymam.android.nilsplus.LicenseService"));
    mLicenseServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = new Messenger(service);
            try {
                // request a license from NiLSPlus app
                Message msg = Message.obtain(null, MSG_REQUEST_LICENSE);
                msg.replyTo = mMessenger;
                mService.send(msg);
            } catch (RemoteException e) {
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };
    try {
        bindService(licenseService, mLicenseServiceConnection, Context.BIND_AUTO_CREATE);
    } catch (Exception exp) {
        Log.d("NiLS", "cannot communicate with NiLS Unlocker");
        exp.printStackTrace();
    }
}

From source file:com.tweetlanes.android.view.HomeActivity.java

void doUnbindService() {
    if (mIsBound) {
        // If we have received the service, and hence registered with it, then now is the time to unregister.
        if (mService != null) {
            try {
                Message msg = Message.obtain(null, BackgroundService.MSG_UNREGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);//from  w w  w. ja  v a  2 s  . c o m
            } catch (RemoteException e) {
                // There is nothing special we need to do if the service has crashed.
            }
        }
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}

From source file:it.angrydroids.epub3reader.MainActivity.java

private void SendMessageToEPDMainService(int what) {
    try {/*from  w  ww  .ja  va2  s  .c  o m*/
        Message msg = Message.obtain(null, what);
        msg.replyTo = mMainActivityMessenger;
        if (mEPDMainService != null) {
            mEPDMainService.send(msg);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}