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: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();
    msg.what = what;//from ww  w .j  av  a2s  .  co  m
    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:com.mindprotectionkit.freephone.RedPhoneService.java

private void sendMessage(int code, Object extra) {
    Message message = Message.obtain();
    message.what = code;/*from   w ww .  j a va2s .c  om*/
    message.obj = extra;

    if (handler != null)
        handler.sendMessage(message);
    else
        bufferedEvents.add(message);
}

From source file:com.droid.app.fotobot.FotoBot.java

/**
 *      ? ,   ?,    ?// ww  w. j  a va2 s  . com
 *
 * @param h
 * @param delay
 */
public void fbpause(final Handler h, final int delay) {

    if (delay > 3) {
        SendMessage(".", MSG_INFO);
    }

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");

    wakeLock.acquire();

    Thread thread = new Thread() {
        public void run() {

            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "MyWakelockTag");

            wakeLock.acquire();

            int n = 5;

            if (delay >= wake_up_interval) {
                n = (int) (delay - ((delay / wake_up_interval) * long_pause * 2));
            }

            for (int i = 1; i <= n; i++) {

                if (i % 3 == 0) {
                    if (isCharging_before != isCharging) {

                        if (isCharging) {
                            SendMessage(getResources().getString(R.string.voltage_restored));
                            if (isCharging_sms)
                                sendSMS(sms_voltage_alert_number,
                                        getResources().getString(R.string.voltage_restored));
                        } else {
                            SendMessage(getResources().getString(R.string.no_voltage));
                            if (isCharging_sms)
                                sendSMS(sms_voltage_alert_number,
                                        getResources().getString(R.string.no_voltage));
                        }

                        isCharging_before = isCharging;

                    }
                }

                Message msg = Message.obtain(); // Creates an new Message instance

                // we make 1 sec for each n
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (getstatus() == 3) {
                    return;
                }

                if (i % 5 == 0 && sms_check_file) {
                    // checking for sms file each 5 seconds during big pause between photos
                    File sms_file = null;

                    //                        sms_file = new File((getApplicationContext().getFilesDir().toString() + "/sms.txt"));
                    sms_file = new File(work_dir + "/sms.txt");

                    if (sms_file.isFile()) {

                        sms = file2array(sms_file.toString());

                        sms_getdata();

                        try {
                            TimeUnit.SECONDS.sleep(3);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        for (int ind = 0; ind < sms.size(); ind++) {
                            SendMessage(sms.get(ind).toString(), MSG_INFO);
                        }

                        SendMessage("SMS:", MSG_INFO);

                        sms_file.delete();

                        sms.clear();

                        sms_update_db = true;

                        if (sms_status && sms_incoming_passwd.equals(sms_passwd)) {
                            sendSMS(sms_sender_num, "Bat: " + battery_level + "%\n" + "Upd: " + Photo_Frequency
                                    + "sec\n" + "Flash: " + Use_Flash + "\n" + "Net: " + network);
                            sms_status = false;
                        }

                        if (sms_net_status && sms_incoming_passwd.equals(sms_passwd)) {
                            sendSMS(sms_sender_num,
                                    "Net: " + network + "\n" + "Ch: " + Network_Channel + "\n" + "Method: "
                                            + Network_Connection_Method + "\n" + "Mail: " + EMail_Recepient
                                            + "\n");
                            sms_net_status = false;
                        }

                        if (sms_email_status && sms_incoming_passwd.equals(sms_passwd)) {
                            sendSMS(sms_sender_num,
                                    "Mail: " + EMail_Recepient + "\n" + "FMail: " + EMail_Sender + "\n"
                                            + "FMailP: " + EMail_Sender_Password + "\n" + "SMTPHost: "
                                            + SMTP_Host + "\n" + "SMTPPort: " + SMTP_Port + "\n");
                            sms_email_status = false;
                        }

                        if (sms_media_status && sms_incoming_passwd.equals(sms_passwd)) {
                            sendSMS(sms_sender_num, "Flash: " + Use_Flash + "\n" + "JPEG: " + JPEG_Compression
                                    + "\n" + "Fc: " + make_photo_fc + "\n" + "Bc: " + make_photo_bc + "\n");
                            sms_media_status = false;
                        }

                        if (sms_incoming_passwd.equals(sms_passwd) && sms_help) {
                            sendSMS(sms_sender_num, sms_commands_list());
                            sms_help = false;
                        }

                        // drop password before next usage
                        sms_incoming_passwd = "";

                        return;

                    } else {

                    }

                }

                if (i % wake_up_interval == 0 && frame_delay) {

                    String cmd = null;

                    try {

                        if (Build.VERSION.SDK_INT <= 12) {
                            cmd = "logcat -v long -d -f " + work_dir + "/logfile.txt" + " Logs:* FotoBot:* *:S";
                        } else {
                            cmd = "logcat -v long -d -f " + work_dir + "/logfile.txt";
                        }
                        Runtime.getRuntime().exec(cmd);

                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.d("LOG_TAG", "logcat2file doesn't work");
                    }

                    try {
                        TimeUnit.SECONDS.sleep(short_pause);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // ?    log_size,  ? 
                    File logcat_file;
                    logcat_file = new File(work_dir + "/logfile.txt");

                    boolean fileExists = logcat_file.isFile();

                    if (fileExists) {

                        if (logcat_file.length() / 1000 > log_size) {
                            clearLog();
                        }

                    } else {
                        SendMessage("logfile.txt doesn't exist.");
                    }

                    SendMessage(".", MSG_INFO);

                    if (camera == null) {
                        Log.d(LOG_TAG, "camera == null");
                        SendMessage("..", MSG_INFO);
                        try {
                            camera = Camera.open();
                            SendMessage(".", MSG_INFO);
                            Log.d(LOG_TAG, "Camera has been successfully opened");
                        } catch (Exception e) {
                            SendMessage("...", MSG_INFO);
                            Log.d(LOG_TAG, "Problem with camera opening");
                            e.printStackTrace();
                        }
                    }

                    try {
                        camera.setPreviewDisplay(holder);
                        camera.startPreview();
                        SendMessage(".", MSG_INFO);
                        Log.d(LOG_TAG, "Preview started");
                    } catch (Exception e) {
                        e.printStackTrace();
                        SendMessage("...", MSG_INFO);
                        Log.d(LOG_TAG, "Problem with starting of preview");
                    }

                    try {
                        TimeUnit.SECONDS.sleep(long_pause);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if (camera != null) {
                        Log.d(LOG_TAG, "Camera is busy");
                        try {
                            camera.stopPreview();
                            camera.release();
                            camera = null;
                            Log.d(LOG_TAG, "Camera unlocked");
                        } catch (Exception e) {
                            SendMessage("...", MSG_INFO);
                        }

                    }

                    try {
                        TimeUnit.SECONDS.sleep(long_pause);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }

    };
    thread.start();

    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    wakeLock.release();

}

From source file:org.ymkm.lib.controller.support.ControlledDialogFragment.java

/**
 * Sends a message to itself/*from  w w w . j  av a2  s . c  o  m*/
 * 
 * @param what
 *            message ID to send to itself
 * @param arg1
 *            first message int argument
 * @param arg2
 *            second message int argment
 * @param obj
 *            Object argument
 * @return {@code true} if message could be sent, {@code false} otherwise
 */
@Override
public final boolean sendToSelf(int what, int arg1, int arg2, Object obj) {
    Message m = Message.obtain();
    m.what = what;
    m.arg1 = arg1;
    m.arg2 = arg2;
    m.obj = obj;
    return getHandler().sendMessage(m);
}

From source file:biz.bokhorst.bpt.BPTService.java

private void sendMessage(int type, Bundle data) {
    try {/*ww  w.  j  a va2  s  .c  om*/
        if (clientMessenger != null) {
            try {
                Message msg = Message.obtain();
                msg.what = type;
                if (data != null)
                    msg.setData(data);
                clientMessenger.send(msg);
            } catch (Exception ex) {
                Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    } catch (Throwable ex) {
        Log.e("BPT", "sendMessage: " + ex);
    }
}

From source file:com.realtek.simpleconfig.SCTest.java

/**  */
@SuppressWarnings("deprecation")
public void DiscoverDevPopUp(final int milliSeconds) {
    discovDialog.setTitle("Wi-Fi: " + SCLib.getConnectedWifiSSID());
    discovDialog.setMessage("    ?......");
    discovDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    discovDialog.setCancelable(false);/*from  ww w .  ja  va2  s .c  o m*/
    discovDialog.setButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            DiscovEnable = false;
            dialog.cancel();
        }
    });
    discovDialog.show();

    new Thread(new Runnable() {
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
            byte[] DiscovCmdBuf = SCCtlOps.rtk_sc_gen_discover_packet(SCLib.rtk_sc_get_default_pin());
            long startTime = System.currentTimeMillis();
            long endTime = System.currentTimeMillis();
            while (DiscovEnable && (endTime - startTime) < milliSeconds) {
                SCLib.rtk_sc_send_discover_packet(DiscovCmdBuf, "255.255.255.255");
                endTime = System.currentTimeMillis();
            }
            Log.i(TAG, "Discover Time Elapsed: " + (endTime - startTime) + "ms");

            // Update Status
            Message msg = Message.obtain();
            msg.obj = null;
            msg.what = ~SCCtlOps.Flag.DiscoverACK; //timeout
            SCLib.TreadMsgHandler.sendMessage(msg);
        }
    }).start();
}

From source file:ee.ioc.phon.android.speak.RecognizerIntentActivity.java

private static Message createMessage(int type, String str) {
    Bundle b = new Bundle();
    b.putString(MSG, str);//from   ww w .ja v  a2  s  .c o  m
    Message msg = Message.obtain();
    msg.what = type;
    msg.setData(b);
    return msg;
}

From source file:com.droid.app.fotobot.FotoBot.java

/**
 *  ?  ?//from  ww  w. ja  va  2  s  . c om
 *
 * @param h   handler,   ??
 * @param str ? ??
 */
public void SendMessage(Handler h, String str) {

    Message msg = Message.obtain(); // Creates an new Message instance
    msg.obj = str; // Put the string into Message, into "obj" field.
    msg.setTarget(h); // Set the Handler
    msg.sendToTarget(); //Send the message

}

From source file:com.droid.app.fotobot.FotoBot.java

/**
 *  ?  ?/*w w  w  . j  av  a2s.  c o m*/
 *
 * @param str ? ??
 */
public void SendMessage(String str) {

    Message msg = Message.obtain(); // Creates an new Message instance
    msg.obj = str; // Put the string into Message, into "obj" field.
    msg.setTarget(h); // Set the Handler
    msg.sendToTarget(); //Send the message

}