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) 

Source Link

Document

Same as #obtain() , but sets the value for the target member on the Message returned.

Usage

From source file:com.espian.ticktock.CountdownFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getArguments() == null)
        throw new IllegalArgumentException("No args supplied for fragment");

    try {//  w w w .j a va 2s .com

        final Date date = DateFormat.getDateInstance(DateFormat.LONG).parse(getArguments().getString("date"));
        mIdAsString = getArguments().getString(BaseColumns._ID);
        mLabelView.setText(mLabel = getArguments().getString("label"));
        mDateView.setText(DateFormat.getDateInstance(DateFormat.MEDIUM).format(date));
        mHelper = new LoadHideHelper(this);

        new Thread(new Runnable() {
            @Override
            public void run() {

                // Requires an ugly fudge because, for some reason, the Days class accesses
                // the disk through random access, which throws errors with StrictMode.
                int days = Days.daysBetween(new DateTime(new Date()), new DateTime(date)).getDays();
                Bundle b = new Bundle();
                b.putString("result", String.valueOf(days + 1));
                Message m = Message.obtain(asyncHandler);
                m.setData(b);
                asyncHandler.sendMessage(m);

            }
        }).start();

    } catch (ParseException e) {
        Toast.makeText(getActivity(), "Malformed date was stored", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

    //getActivity().getLoaderManager().initLoader(TickTockProvider.LOADER_SINGLE_ITEM, getArguments(), this);

}

From source file:org.chromium.android_webview.test.AwContentsTest.java

private int callDocumentHasImagesSync(final AwContents awContents) throws Throwable, InterruptedException {
    // Set up a container to hold the result object and a semaphore to
    // make the test wait for the result.
    final AtomicInteger val = new AtomicInteger();
    final Semaphore s = new Semaphore(0);
    final Message msg = Message.obtain(new Handler(Looper.getMainLooper()) {
        @Override/*from   w w w . j  a v  a 2s .co  m*/
        public void handleMessage(Message msg) {
            val.set(msg.arg1);
            s.release();
        }
    });
    runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            awContents.documentHasImages(msg);
        }
    });
    assertTrue(s.tryAcquire(WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    int result = val.get();
    return result;
}

From source file:com.zuzhili.bussiness.helper.CCPHelper.java

/**
 * send object to activity by handler./*ww w  . j a v  a  2s.c om*/
 * 
 * @param what
 *            message id of handler
 * @param obj
 *            message of handler
 */
private void sendTarget(int what, Object obj) {
    t = System.currentTimeMillis();
    // for kinds of mobile phones
    while (handler == null && (System.currentTimeMillis() - t < 3200)) {
        Log4Util.d(DEMO_TAG, "[VoiceHelper] handler is null, activity maybe destory, wait...");
        try {
            Thread.sleep(80L);
        } catch (InterruptedException e) {
        }
    }

    if (handler == null) {
        Log4Util.d(DEMO_TAG, "[VoiceHelper] handler is null, need adapter it.");
        return;
    }

    Message msg = Message.obtain(handler);
    msg.what = what;
    msg.obj = obj;
    msg.sendToTarget();
}

From source file:com.android.mms.ui.MessageUtils.java

public static void viewSimpleSlideshow(Context context, SlideshowModel slideshow) {
    if (!slideshow.isSimple()) {
        throw new IllegalArgumentException("viewSimpleSlideshow() called on a non-simple slideshow");
    }/* w w  w  . j  ava 2  s .c om*/
    SlideModel slide = slideshow.get(0);
    MediaModel mm = null;
    if (slide.hasImage()) {
        mm = slide.getImage();
    } else if (slide.hasVideo()) {
        mm = slide.getVideo();
    } else if (slide.hasAudio()) {
        mm = slide.getAudio();
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra("SingleItemOnly", true); // So we don't see "surrounding" images in Gallery
    /// M: CanShare: false, Hide the videopalye's share option menu.
    /// M: CanShare: ture, Show the share option menu.
    /// M: Code analyze 010, For fix bug ALPS00244046, The "JE" pops up
    // after you tap the "messaging" icon. @{
    intent.putExtra("CanShare", false);
    /// @}
    /// M: for showing notification when view mms with video player in full screen model @{
    intent.putExtra(EXTRA_FULLSCREEN_NOTIFICATION, true);
    /// @}

    String contentType = "";
    if (mm != null) {
        contentType = mm.getContentType();
        MmsLog.e(TAG, "viewSimpleSildeshow. Uri:" + mm.getUri());
        MmsLog.e(TAG, "viewSimpleSildeshow. contentType:" + contentType);
        intent.setDataAndType(getPreviewFileUri(context, mm), contentType);
    }
    /// M: Code analyze 013, For fix bug ALPS00250939, Exception/Java(JE)-->com.android.mms.
    try {
        // M: change feature ALPS01751464
        if (mm != null && mm.hasDrmContent()) {
            DrmUtilsEx.showDrmAlertDialog(context);
            return;
        }

        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Message msg = Message.obtain(MmsApp.getToastHandler());
        msg.what = MmsApp.MSG_MMS_CAN_NOT_OPEN;
        msg.obj = contentType;
        msg.sendToTarget();
        /// M: after user click view, and the error toast is shown,
        /// we must make it can press again. tricky code
        if (context instanceof ComposeMessageActivity) {
            ((ComposeMessageActivity) context).mClickCanResponse = true;
        }
    }
    /// @}
}

From source file:com.ringdroid.RingdroidEditActivity.java

private void afterSavingRingtone(CharSequence title, String outPath, File outFile, int duration) {
    long length = outFile.length();
    if (length <= 512) {
        outFile.delete();//from   w w w.j a v  a  2 s.  c om
        new AlertDialog.Builder(this).setTitle(R.string.alert_title_failure)
                .setMessage(R.string.too_small_error).setPositiveButton(R.string.alert_ok_button, null)
                .setCancelable(false).show();
        return;
    }

    // Create the database record, pointing to the existing file path

    long fileSize = outFile.length();
    String mimeType = "audio/mpeg";

    String artist = "" + getResources().getText(R.string.artist_name);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, outPath);
    values.put(MediaStore.MediaColumns.TITLE, title.toString());
    values.put(MediaStore.MediaColumns.SIZE, fileSize);
    values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);

    values.put(MediaStore.Audio.Media.ARTIST, artist);
    values.put(MediaStore.Audio.Media.DURATION, duration);

    values.put(MediaStore.Audio.Media.IS_RINGTONE, mNewFileKind == FileSaveDialog.FILE_KIND_RINGTONE);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION);
    values.put(MediaStore.Audio.Media.IS_ALARM, mNewFileKind == FileSaveDialog.FILE_KIND_ALARM);
    values.put(MediaStore.Audio.Media.IS_MUSIC, mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC);

    // Insert it into the database
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);
    final Uri newUri = getContentResolver().insert(uri, values);
    setResult(RESULT_OK, new Intent().setData(newUri));

    // Update a preference that counts how many times we've
    // successfully saved a ringtone or other audio
    SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
    int successCount = prefs.getInt(PREF_SUCCESS_COUNT, 0);
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putInt(PREF_SUCCESS_COUNT, successCount + 1);
    prefsEditor.commit();

    // If Ringdroid was launched to get content, just return
    if (mWasGetContentIntent) {
        // sendStatsToServerIfAllowedAndFinish();
        return;
    }

    // There's nothing more to do with music or an alarm. Show a
    // success message and then quit.
    if (mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC || mNewFileKind == FileSaveDialog.FILE_KIND_ALARM) {
        Toast.makeText(this, R.string.save_success_message, Toast.LENGTH_SHORT).show();
        // sendStatsToServerIfAllowedAndFinish();
        return;
    }

    // If it's a notification, give the user the option of making
    // this their default notification. If they say no, we're finished.
    if (mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION) {
        new AlertDialog.Builder(RingdroidEditActivity.this).setTitle(R.string.alert_title_success)
                .setMessage(R.string.set_default_notification)
                .setPositiveButton(R.string.alert_yes_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        RingtoneManager.setActualDefaultRingtoneUri(RingdroidEditActivity.this,
                                RingtoneManager.TYPE_NOTIFICATION, newUri);
                        // sendStatsToServerIfAllowedAndFinish();
                    }
                }).setNegativeButton(R.string.alert_no_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // sendStatsToServerIfAllowedAndFinish();
                    }
                }).setCancelable(false).show();
        return;
    }

    // If we get here, that means the type is a ringtone. There are
    // three choices: make this your default ringtone, assign it to a
    // contact, or do nothing.

    final Handler handler = new Handler() {
        public void handleMessage(Message response) {
            int actionId = response.arg1;
            switch (actionId) {
            case R.id.button_make_default:
                RingtoneManager.setActualDefaultRingtoneUri(RingdroidEditActivity.this,
                        RingtoneManager.TYPE_RINGTONE, newUri);
                Toast.makeText(RingdroidEditActivity.this, R.string.default_ringtone_success_message,
                        Toast.LENGTH_SHORT).show();
                // sendStatsToServerIfAllowedAndFinish();
                break;
            case R.id.button_choose_contact:
                chooseContactForRingtone(newUri);
                break;
            default:
            case R.id.button_do_nothing:
                // sendStatsToServerIfAllowedAndFinish();
                break;
            }
        }
    };
    Message message = Message.obtain(handler);
    AfterSaveActionDialog dlog = new AfterSaveActionDialog(this, message);
    dlog.show();
}

From source file:mobi.omegacentauri.ptimer.PTimerEditActivity.java

private void afterSavingRingtone(CharSequence title, String outPath, File outFile, int duration) {
    long length = outFile.length();
    if (length <= 512) {
        outFile.delete();//w  w w.j  a v a 2 s  .  c om
        new AlertDialog.Builder(this).setTitle(R.string.alert_title_failure)
                .setMessage(R.string.too_small_error).setPositiveButton(R.string.alert_ok_button, null)
                .setCancelable(false).show();
        return;
    }

    // Create the database record, pointing to the existing file path

    long fileSize = outFile.length();
    String mimeType = "audio/mpeg";

    String artist = "" + getResources().getText(R.string.artist_name);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, outPath);
    values.put(MediaStore.MediaColumns.TITLE, title.toString());
    values.put(MediaStore.MediaColumns.SIZE, fileSize);
    values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);

    values.put(MediaStore.Audio.Media.ARTIST, artist);
    values.put(MediaStore.Audio.Media.DURATION, duration);

    values.put(MediaStore.Audio.Media.IS_RINGTONE, mNewFileKind == FileSaveDialog.FILE_KIND_RINGTONE);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION);
    values.put(MediaStore.Audio.Media.IS_ALARM, mNewFileKind == FileSaveDialog.FILE_KIND_ALARM);
    values.put(MediaStore.Audio.Media.IS_MUSIC, mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC);

    // Insert it into the database
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);
    final Uri newUri = getContentResolver().insert(uri, values);
    setResult(RESULT_OK, new Intent().setData(newUri));

    // Update a preference that counts how many times we've
    // successfully saved a ringtone or other audio
    SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
    int successCount = prefs.getInt(PREF_SUCCESS_COUNT, 0);
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putInt(PREF_SUCCESS_COUNT, successCount + 1);
    prefsEditor.commit();

    // If Ringdroid was launched to get content, just return
    if (mWasGetContentIntent) {
        sendStatsToServerIfAllowedAndFinish();
        return;
    }

    // There's nothing more to do with music or an alarm.  Show a
    // success message and then quit.
    if (mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC || mNewFileKind == FileSaveDialog.FILE_KIND_ALARM) {
        Toast.makeText(this, R.string.save_success_message, Toast.LENGTH_SHORT).show();
        sendStatsToServerIfAllowedAndFinish();
        return;
    }

    // If it's a notification, give the user the option of making
    // this their default notification.  If they say no, we're finished.
    if (mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION) {
        new AlertDialog.Builder(PTimerEditActivity.this).setTitle(R.string.alert_title_success)
                .setMessage(R.string.set_default_notification)
                .setPositiveButton(R.string.alert_yes_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        RingtoneManager.setActualDefaultRingtoneUri(PTimerEditActivity.this,
                                RingtoneManager.TYPE_NOTIFICATION, newUri);
                        sendStatsToServerIfAllowedAndFinish();
                    }
                }).setNegativeButton(R.string.alert_no_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        sendStatsToServerIfAllowedAndFinish();
                    }
                }).setCancelable(false).show();
        return;
    }

    // If we get here, that means the type is a ringtone.  There are
    // three choices: make this your default ringtone, assign it to a
    // contact, or do nothing.

    final Handler handler = new Handler() {
        public void handleMessage(Message response) {
            int actionId = response.arg1;
            switch (actionId) {
            case R.id.button_make_default:
                RingtoneManager.setActualDefaultRingtoneUri(PTimerEditActivity.this,
                        RingtoneManager.TYPE_RINGTONE, newUri);
                Toast.makeText(PTimerEditActivity.this, R.string.default_ringtone_success_message,
                        Toast.LENGTH_SHORT).show();
                sendStatsToServerIfAllowedAndFinish();
                break;
            case R.id.button_choose_contact:
                chooseContactForRingtone(newUri);
                break;
            default:
            case R.id.button_do_nothing:
                sendStatsToServerIfAllowedAndFinish();
                break;
            }
        }
    };
    Message message = Message.obtain(handler);
    AfterSaveActionDialog dlog = new AfterSaveActionDialog(this, message);
    dlog.show();
}

From source file:com.tsp.clipsy.audio.RingdroidEditActivity.java

private void onSave() {
    if (mIsPlaying) {
        handlePause();//w  w  w  . j a v  a 2s.c  o  m
    }

    final Handler handler = new Handler() {
        public void handleMessage(Message response) {
            CharSequence newTitle = (CharSequence) response.obj;
            saveRingtone(newTitle);
        }
    };
    Message message = Message.obtain(handler);
    FileSaveDialog dlog = new FileSaveDialog(this, getResources(), mTitle, message);
    dlog.show();
}

From source file:com.SpeechEd.SpeechEdEditActivity.java

private void afterSavingRingtone(CharSequence title, String outPath, File outFile, int duration) {
    long length = outFile.length();
    if (length <= 512) {
        outFile.delete();//from  ww  w  .ja v  a  2  s.c o m
        new AlertDialog.Builder(this).setTitle(R.string.alert_title_failure)
                .setMessage(R.string.too_small_error).setPositiveButton(R.string.alert_ok_button, null)
                .setCancelable(false).show();
        return;
    }

    // Create the database record, pointing to the existing file path

    long fileSize = outFile.length();
    String mimeType = "audio/mpeg";

    String artist = "" + getResources().getText(R.string.artist_name);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, outPath);
    values.put(MediaStore.MediaColumns.TITLE, title.toString());
    values.put(MediaStore.MediaColumns.SIZE, fileSize);
    values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);

    values.put(MediaStore.Audio.Media.ARTIST, artist);
    values.put(MediaStore.Audio.Media.DURATION, duration);

    values.put(MediaStore.Audio.Media.IS_RINGTONE, mNewFileKind == FileSaveDialog.FILE_KIND_RINGTONE);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION);
    values.put(MediaStore.Audio.Media.IS_ALARM, mNewFileKind == FileSaveDialog.FILE_KIND_ALARM);
    values.put(MediaStore.Audio.Media.IS_MUSIC, mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC);

    // Insert it into the database
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);
    final Uri newUri = getContentResolver().insert(uri, values);
    setResult(RESULT_OK, new Intent().setData(newUri));

    // Update a preference that counts how many times we've
    // successfully saved a ringtone or other audio
    SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
    int successCount = prefs.getInt(PREF_SUCCESS_COUNT, 0);
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putInt(PREF_SUCCESS_COUNT, successCount + 1);
    prefsEditor.commit();

    // If SpeechEd was launched to get content, just return
    if (mWasGetContentIntent) {
        sendStatsToServerIfAllowedAndFinish();
        return;
    }

    // There's nothing more to do with music or an alarm.  Show a
    // success message and then quit.
    if (mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC || mNewFileKind == FileSaveDialog.FILE_KIND_ALARM) {
        Toast.makeText(this, R.string.save_success_message, Toast.LENGTH_SHORT).show();
        sendStatsToServerIfAllowedAndFinish();
        return;
    }

    // If it's a notification, give the user the option of making
    // this their default notification.  If they say no, we're finished.
    if (mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION) {
        new AlertDialog.Builder(SpeechEdEditActivity.this).setTitle(R.string.alert_title_success)
                .setMessage(R.string.set_default_notification)
                .setPositiveButton(R.string.alert_yes_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        RingtoneManager.setActualDefaultRingtoneUri(SpeechEdEditActivity.this,
                                RingtoneManager.TYPE_NOTIFICATION, newUri);
                        sendStatsToServerIfAllowedAndFinish();
                    }
                }).setNegativeButton(R.string.alert_no_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        sendStatsToServerIfAllowedAndFinish();
                    }
                }).setCancelable(false).show();
        return;
    }

    // If we get here, that means the type is a ringtone.  There are
    // three choices: make this your default ringtone, assign it to a
    // contact, or do nothing.

    final Handler handler = new Handler() {
        public void handleMessage(Message response) {
            int actionId = response.arg1;
            switch (actionId) {
            case R.id.button_make_default:
                RingtoneManager.setActualDefaultRingtoneUri(SpeechEdEditActivity.this,
                        RingtoneManager.TYPE_RINGTONE, newUri);
                Toast.makeText(SpeechEdEditActivity.this, R.string.default_ringtone_success_message,
                        Toast.LENGTH_SHORT).show();
                sendStatsToServerIfAllowedAndFinish();
                break;
            case R.id.button_choose_contact:
                chooseContactForRingtone(newUri);
                break;
            default:
            case R.id.button_do_nothing:
                sendStatsToServerIfAllowedAndFinish();
                break;
            }
        }
    };
    Message message = Message.obtain(handler);
    AfterSaveActionDialog dlog = new AfterSaveActionDialog(this, message);
    dlog.show();
}

From source file:com.taobao.weex.bridge.WXBridgeManager.java

private void sendMessage(String instanceId, int what) {
    Message msg = Message.obtain(mJSHandler);
    msg.obj = instanceId;
    msg.what = what;
    msg.sendToTarget();
}

From source file:com.ringdroid.RingdroidEditActivity.java

private void onSave() {
    if (mIsPlaying) {
        handlePause();/*from w  ww  .  j ava 2s  .com*/
    }

    final Activity activity = this;
    final Handler finishHandler = new Handler() {
        public void handleMessage(Message response) {
            activity.finish();
        }
    };
    final Handler handler = new Handler() {
        public void handleMessage(Message response) {
            CharSequence newTitle = (CharSequence) response.obj;
            mNewFileKind = response.arg1;
            saveRingtone(newTitle);
        }
    };
    Message message = Message.obtain(handler);
    FileSaveDialog dlog = new FileSaveDialog(this, getResources(), Utils.convertGBK(mTitle), message);
    dlog.show();
}